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

I have to delete another file using git rm abc.c But I deleted the wrong one. How can I restore it?

Right now when I give out git status it says

 deleted: abc.c 

By the way, now I have other uncommitted changes.

+10
source share
3 answers

You need to make two commands, the first one will “not erase” the file (removes it from the list of files that are ready for fixing). Then you delete the deletion.

If you read the output of the git status command (after using git rm ), it will actually tell you how to discard the changes (execute the git status after each step to see this).

Poor file:

git reset HEAD <filename>

Restore it (cancel deletion):

git checkout -- <filename>

+19
source

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 the working tree:

 git checkout -- abc.c 
+9
source

If you accidentally did

 git rm -rf . 

and by deleting everything, you can restore it by doing this.

 git status git reset HEAD . git checkout -- . 

first do git status to see which files you deleted. the second resets the HEAD to delete all files with git reset HEAD . finally, you can recover all files by doing git checkout -- .

0
source

All Articles