I accidentally removed the entire directory of my source code...with a nice rm -r. I know, really bad; but fortunately, I had a git repo in the containing directory. Thus, git has a huge list of unstaged changes of deleted files. For example:
"deleted: src/caronmonitor/server.py"
How do I get these files back? There is advice all over the web to do:
git checkout file
or
git revert <commit>
But as I understand that will restore the file to it's state at the last commit. I don't want to go back to the last commit but instead go back to right before the delete operation. I can look in the gitk and see my files as they were before the delete; thus this must be possible.
Yes, You should do a git checkout filename
If you have multiple files, and want to restore all of them back, use git checkout .
from the root of your git directory.
A checkout will restore the files to last version that was added/committed; if a file hadn't been added or committed yet, than 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