Restore deleted file not staged in git
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:
int main(argc[] args){
System.out.println("example");
}
This was your last change that was commited or staged.
Now you edit 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 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:
int main(argc[] args){
System.out.println("example");
}
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