How to revert a "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, i.e.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.
Update:
Since git rm .
deletes all files in this and child directories in the working checkout as well as in the index, you need to undo each of these changes:
git reset HEAD . # This undoes the index changes
git checkout . # This checks out files in this and child directories from the HEAD
This should do what you want. It does not affect parent folders of your checked-out code or index.
Old answer that wasn't:
reset HEAD
will do the trick, and will not erase any uncommitted changes you have made to your files.
after that you need to repeat any git add
commands you had queued up.