Git deleted everything, how to recover files and folders

The first time I used git, I wanted to import an existing project into github, and everything was deleted. After finding the answer, I think git deleted the files after git pull, I try to recover files and folders, but I can not find how to do this.

I did exactly the following:

jesus@jesus-K52F :~/Escritorio/Django/Ujixy$ git init Initialized empty Git repository in /home/jesus/Escritorio/Django/Ujixy/.git/ jesus@jesus-K52F :~/Escritorio/Django/Ujixy$ git add . jesus@jesus-K52F :~/Escritorio/Django/Ujixy$ git status # On branch master # # Initial commit # # Changes to be committed: # (use "git rm --cached <file>..." to unstage) # # new file: Catalogoapp/__init__.py # new file: Catalogoapp/models.py # new file: Catalogoapp/tests.py # new file: Catalogoapp/views.py # new file: Messageapp/__init__.py # new file: Messageapp/models.py # new file: Messageapp/tests.py # new file: Messageapp/views.py # new file: Ujixyapp/__init__.py [...] jesus@jesus-K52F :~/Escritorio/Django/Ujixy$ git push origin master fatal: 'origin' does not appear to be a git repository fatal: The remote end hung up unexpectedly jesus@jesus-K52F :~/Escritorio/Django/Ujixy$ git add * jesus@jesus-K52F :~/Escritorio/Django/Ujixy$ git status # On branch master # # Initial commit # # Changes to be committed: # (use "git rm --cached <file>..." to unstage) # # new file: Catalogoapp/__init__.py # new file: Catalogoapp/models.py # new file: Catalogoapp/tests.py # new file: Catalogoapp/views.py # new file: Messageapp/__init__.py # new file: Messageapp/models.py # new file: Messageapp/tests.py # new file: Messageapp/views.py # new file: Ujixyapp/__init__.py [...] jesus@jesus-K52F :~/Escritorio/Django/Ujixy$ git remote add origin https://github.com/PEREYO/Ujixy.git jesus@jesus-K52F :~/Escritorio/Django/Ujixy$ git pull origin master remote: Counting objects: 3, done. remote: Total 3 (delta 0), reused 0 (delta 0) Unpacking objects: 100% (3/3), done. From https://github.com/PEREYO/Ujixy * branch master -> FETCH_HEAD jesus@jesus-K52F :~/Escritorio/Django/Ujixy$ git push origin master Username for 'https://github.com': PEREYO Password for 'https:// PEREYO@github.com ': Everything up-to-date jesus@jesus-K52F :~/Escritorio/Django/Ujixy$ git init Reinitialized existing Git repository in /home/jesus/Escritorio/Django/Ujixy/.git/ jesus@jesus-K52F :~/Escritorio/Django/Ujixy$ git add * jesus@jesus-K52F :~/Escritorio/Django/Ujixy$ git status # On branch master nothing to commit (working directory clean) 

Now I am trying to fix this by doing the following:

 jesus@jesus-K52F :~/Escritorio/Ujixy$ git fsck --lost-found Checking object directories: 100% (256/256), done. dangling tree bfe11a30d57a0233d3b0c840a3b66f6421987304 jesus@jesus-K52F :~/Escritorio/Ujixy$ git status # On branch master nothing to commit (working directory clean) jesus@jesus-K52F :~/Escritorio/Ujixy$ git reflog 61daa69 HEAD@ {0}: initial pull jesus@jesus-K52F :~/Escritorio/Ujixy$ git cat-file -p bfe11a30d57a0233d3b0c840a3b66f6421987304 040000 tree 9196501a346cfe4347f46d82936745b78b0235b9 Catalogoapp 040000 tree 49561b4bd6adb8fe8bb1915d6bef09cd49195a97 Messageapp 040000 tree 0fb58bf9b56397443fb235e2a38045d6df7cd473 Ujixyapp 100644 blob e69de29bb2d1d6434b8b29ae775ad8c2e48c5391 __init__.py 100644 blob dfe3388ddf2d5ba34559eb3ec56199d83cdce8bd __init__.pyc 100644 blob bcdd55e27be9447bf6b224b8ba0cbc6802509862 manage.py 100644 blob 34c5978d8026844038d530b491828398bc3ea6c7 settings.py 100644 blob 167a6b1965426ec30c25535fe27338b61b2ae0cf settings.pyc 100644 blob 4a7215cb90ae95d64ca30fde1c1277e0155eb4ed urls.py 100644 blob 6eedcddafbc8854f70f44181edac8e63781cfb09 urls.pyc 

But, how can I restore directories with all files and folders? Now I am working in a copy of the .git folder to avoid other problems.

+6
source share
3 answers

Since you already have a link to a tattered tree object, you are well on your way. The following should work: first restore the ragged tree to the Git index:

 git read-tree bfe11a30d57a0233d3b0c840a3b66f6421987304 

Then update the working directory from the updated index:

 git checkout-index -a 
+10
source

Since you can run git cat-file -p on a ragged tree, you can restore it. There are many ways to do this, I will describe 2 that I can quickly think of:

  • Create a new commit to enter files into the ragged tree. This commit will not have a parent.

     echo "A commit to recover the dangling tree." | git commit-tree bfe11a30d57a0233d3b0c840a3b66f6421987304 # Output: <SOME_NEWLY_CREATED_COMMIT_SHA1> 

    The new commit should contain the work tree of the tree of ragged trees that you just learned. The output of the above command should show the newly created SHA1 that was created.

    To switch the current working tree to this:

     git checkout <SOME_NEWLY_CREATED_COMMIT_SHA1> 

    Now you should be able to see all the files and contents that were indicated in the tree battle. You can browse, backup files, do whatever you want;)

  • Alternative approach:

    If you just want to make changes on top of your current commit, this approach might be useful.

    Read the contents of the tree into the git index (i.e. into the staging area for this case).

     git read-tree bfe11a30d57a0233d3b0c840a3b66f6421987304 

    Now commit the changes in the staging area on top of the current marked branch:

     git commit -m "Recover the lost files." 

And for the future:

  • Always commit your changes, it’s much easier to get them (using reflogs), even if the fixation hangs in the future. If you are in doubt about using git commit , you can always compensate for the commit, make changes, rewrite history, etc. Especially before executing commands like git pull or git push you should make your changes so that they are not lost.

  • Do not run git init twice in the repository, although git is smart enough to know that the repo is already initialized and is not trying to overwrite your changes.

+2
source

I do not believe that you can recover these files and folders if you did not transfer them. Git can restore everything that you did in the repo, but if you do not do it in the first place, it is not included in the repo. This is partly the reason I like to use Git in the Dropbox folder.

-2
source

Source: https://habr.com/ru/post/944403/


All Articles