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

30.2k views Asked by At

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.

4

There are 4 answers

0
rob mayoff On

First you need to reset the status of abc.c in the index:

git reset -- abc.c

Then you need to restore abc.c in your working tree:

git checkout -- abc.c
0
Sunil D. On

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>

0
Gastón Saillén On

If you accidentally did

git rm -rf .

and removed all, you can recover it doing this.

git status
git reset HEAD .
git checkout -- .

first do git status to see what files you have deleted. second reset the HEAD to unstage all the files with git reset HEAD . lastly, you can restore all the files by doing git checkout -- .

0
SAROJ KUMAR BARIK On

If you accidentally delete the file using git delete command 'rm' and want to recover, then use the command :

git reset HEAD^<filename>

to recover the file