Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

git

git-rm

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.

like image 952
Anders Lind Avatar asked Jul 30 '12 18:07

Anders Lind


3 Answers

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>

like image 199
Sunil D. Avatar answered Oct 11 '22 23:10

Sunil D.


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
like image 37
rob mayoff Avatar answered Oct 11 '22 23:10

rob mayoff


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 -- .

like image 43
Gastón Saillén Avatar answered Oct 12 '22 00:10

Gastón Saillén