'git undo deleted files

I am new to git. I have checkout files from remote. I had to delete few files from the git repo. Instead of doing git rm command, I issued unix rm -rf folder command. I need to revert the delete command and then perform git rm command. How to revert to the latest code?

Note: I have not yet committed the staged files.The out out of git status is the list of files deleted in the below format:

#   deleted:    i18n/angular-locale_sl.js
#   deleted:    i18n/angular-locale_in.js 


Solution 1:[1]

I need to revert the delete command and then perform git rm command. How to revert to the latest code?

Simply do a (since you haven't committed anything):

cd /root/of/your/repo
git checkout HEAD -- .

That will restore the working tree to the index.

(A git reset --hard should work too, but isn't needed here)

But you could also register those deletion to the index directly:

git add -A .

See "What's the difference between git add . and git add -u?"

Solution 2:[2]

If you have staged the deletion, first unstage it:

git reset HEAD path/to/deleted/file

Then restore the file:

git checkout path/to/deleted/file

Solution 3:[3]

Revert a git delete on your local machine

You're wanting to undo of git rm or rm followed by git add:

Restore the file in the index:

git reset -- <file>

after that check out from index

git checkout -- <file>

for example:

git reset -- src/main/java/de/foo/bar.java
git checkout -- src/main/java/de/foo/bar.java

Solution 4:[4]

To supplement the answer by @VonC,

I had to delete few files from the git repo. Instead of doing git rm command, I issued unix rm -rf folder command

The only difference between the two options is that git rm can also remove the files from your git index. However, because you never added them to the index to begin with, there is no particular reason to use git rm, and you don't need to undo the plain /bin/rm.

Solution 5:[5]

I deleted a folder without committing the change. I was able to recover the folder by simply using git recover

git recover name_of_deleted_folder

I had to spell the full name, that is partial_name_of_deleted_folder* did not work.

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Community
Solution 2 Damien Bezborodow
Solution 3 Sma Ma
Solution 4 mockinterface
Solution 5 audiracmichelle