How to recover file after `git rm abc.c`?

Anders Lind picture Anders Lind · Jul 30, 2012 · Viewed 19.5k times · Source

I supposed to delete another file with git rm abc.c. But I deleted a wrong one. How can I recover it?

Right now, when I issue git status, it says

deleted:   abc.c

BTW, I have other uncommitted changes now.

Answer

Sunil D. picture Sunil D. · Jul 30, 2012

You need to do two commands, the first will "unstage" the file (removes it from the list of files that are ready to be committed). Then, you undo the delete.

If you read the output of the git status command (after the using git rm), it actually tells you how to undo the changes (do a git status after each step to see this).

Unstage the file:

git reset HEAD <filename>

Restore it (undo the delete):

git checkout -- <filename>