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 won't have stored any changes that were never added or committed. The best you can do is git checkout that deleted directory, and you'll get it back in the state it was in when things were last committed.
git checkout <branch_you_are_in> <directory_of_all_your_source_code>
The local uncommitted changes you are seeing is probably the uncommitted deletion, i.e. git sees that you've deleted a bunch of files, but you haven't committed the fact that they are deleted yet, so git says 'hey, there are some changes (in this case, deletions) you haven't committed.'
Not, GIT does not do any magic. If you did not staged or commited your changes then those are gone. You can see deletions but you can only revert those to last state that you told GIT to remember it for you.
You have to tell explicitly GIT to remember your changes by staging and commiting.
So if you have file.txt in your repository with content:
This was your last change that was commited or staged.
Now you edit file.txt to contain something like:
You save your file, close editor, do nothing with git and do
rm -r
Now the file is there, and git has reference to file and that it was deleted but content of this file in git is only: