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.
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>
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
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 -- .
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With