Recover deleted file not installed in git

I accidentally deleted the entire directory of my source code ... with good rm -r. I know very badly; but, fortunately, I had a git repository in the containing directory. Thus, git has a huge list of uninstalled changes to deleted files. For instance:

"deleted: src/caronmonitor/server.py" 

How to return these files? There are tips all over the internet:

 git checkout file 

or

 git revert <commit> 

But since I understand that it will restore the file, it will indicate the last commit. I don't want to go back to the last commit, but instead go back to the delete operation. I can look in gitk and look at my files as they were before the deletion; thus, it should be possible.

+6
source share
3 answers

No, GIT does not do magic . If you did not make or did not make your changes, they disappeared. You can see the deletions, but you can only return them to the last state that you told GIT to remember it for you.

You must explicitly specify GIT to remember your changes by staging and making.

So, if you have a .txt file in your repository with content:

  int main(argc[] args){ System.out.println("example"); } 

This is your last change that has been made or delivered.

Now you are editing file.txt to contain something like:

 int main(argc[] args){ System.out.println("example"); System.out.println("Hey I can print more lines"); } 

You save your file, close the editor, do nothing with GIT and do rm -r Now there is a file, and GIT has a link to the file and that it was deleted, but the contents of this file in GIT only:

  int main(argc[] args){ System.out.println("example"); } 
+3
source

Yes do a git checkout filename

If you have several files and want to restore them all, use git checkout . from the root of your git directory.

The output will restore files to the latest version that has been added / committed; if the file has not yet been added or fixed, it will be lost.

So for example:

 $ git init && touch test1.txt test2.txt test3.txt $ git add test1.txt && git commit -m "test1" && git add test2.txt $ ls -a . .. .git test1.txt test2.txt test3.txt #deleting files below, 2 will be recovered, 3rd will be gone. $ rm * $ ls -a . .. .git $ git checkout . $ ls -a . .. .git test1.txt test2.txt #test3.txt is gone, test2.txt was recovered, even though not committed but just added 
+9
source

git will not store any changes that have never been added or have not been added. The best you can do is git checkout, which deleted the directory, and you will return it in the state in which it was when the cases were last.

git checkout <branch_you_are_in> <directory_of_all_your_source_code>

The local uncommitted changes that you see are probably uncommitted deletions, i.e. git sees that you deleted a bunch of files, but you have not yet recorded the fact of their deletion, so git says "Hey, there are some changes (in this case, deletion) that you have not made. '

0
source

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


All Articles