How to revert a "git rm

I accidentely said git rm -r . . How do I recover from this?

I did not commit.

I think all files were marked for deletion and were also physically removed from my local checkout.

EDIT: I could (if I knew the command) revert to the last commit. But it would be a lot better if I could just undo the git rm -r . . Because I am not really sure what I did after the last commit and before the git rm -r . .


git reset HEAD

Should do it. If you don't have any uncommitted changes that you care about, then

git reset --hard HEAD

should forcibly reset everything to your last commit. If you do have uncommitted changes, but the first command doesn't work, then save your uncommitted changes with git stash :

git stash
git reset --hard HEAD
git stash pop

I git-rm'd a few files and went on making changes before my next commit when I realized I needed some of those files back. Rather than stash and reset, you can simply checkout the individual files you missed/removed if you want:

git checkout HEAD path/to/file path/to/another_file

This leaves your other uncommitted changes intact with no workarounds.


To regain some single files or folders one may use the following

git reset -- path/to/file
git checkout -- path/to/file

This will first recreate the index entries for path/to/file and recreate the file as it was in the last commit, ie HEAD .

Hint: one may pass a commit hash to both commands to recreate files from an older commit. See git reset --help and git checkout --help for details.

链接地址: http://www.djcxy.com/p/4006.html

上一篇: .gitignore不起作用

下一篇: 如何恢复“git rm