How to revert a "git rm -r ."?

292,309

Solution 1

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

Solution 2

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.

Solution 3

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.

Solution 4

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.

Solution 5

If you end up with none of the above working, you might be able to retrieve data using the suggestion from here: http://www.spinics.net/lists/git/msg62499.html

git prune -n
git cat-file -p <blob #>
Share:
292,309
user89021
Author by

user89021

Updated on July 08, 2022

Comments

  • user89021
    user89021 almost 2 years

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

  • Alex Brown
    Alex Brown about 14 years
    Note that git reset --hard HEAD destroys any useful changes you have made in parent directories of the current working directory.
  • hoipolloi
    hoipolloi over 12 years
    @Mild: I'm still wearing a cold sweat!
  • Greg M. Krsak
    Greg M. Krsak about 12 years
    I didn't down vote, but I just tried stash, reset hard, pop and lost all of my recent changes. Maybe I misread the answer.
  • Alex Brown
    Alex Brown almost 12 years
    sorry, I always setup git alias.co="checkout" so that git co does checkout.
  • Nuby
    Nuby about 11 years
    This rarely works for me, and I'm so glad I work in a Dropbox folder. Poor form, but saves me every time...
  • Dan
    Dan over 9 years
    This helped because I had other uncommitted changes.
  • sudo
    sudo almost 9 years
    When I did git stash pop, it just removed the files again, of course because it stashes the fact that I (accidentally) removed some files. This answer doesn't work if you have uncommited changes you want to keep.
  • Xonatron
    Xonatron over 8 years
    This also did not work for me. The files are still missing.
  • niry
    niry about 8 years
    This does not answer the question. git rm -r . could be executed in any sub-directly. This will reset the entire tree. See @Jaime Bellmyer answer.
  • tresf
    tresf about 8 years
    This answer helps for those of us that stumbled upon this question looking to revert a single git rm rather than an entire recursive git rm -r. For a full recursive delete, the other solutions may be better, depending on the amount of files removed.
  • mushcraft
    mushcraft almost 8 years
    This worked for 'Changes to be committed:' where a file was deleted from an old git cherry-pick which I still needed.I had tried git reset HEAD~<number> <file> which didn't work.
  • ThievingSix
    ThievingSix over 7 years
    4 years later, still a lifesaver!
  • jpaugh
    jpaugh about 6 years
    Of course, nothing can undo git rm -rf, as that may delete untracked or staged (only) files as well.
  • Admin
    Admin about 6 years
    git rm -rf file and git rm -rf dir will not delete any untracked files.
  • sam
    sam about 6 years
    You may need git stash -u/git stash --include-untracked if your recent changes include new (untracked) files.
  • Marcin Wolniewicz
    Marcin Wolniewicz almost 6 years
    @sudo Instead of using git stash pop you can use git stash apply. This way if there's a merge conflict or you want to discard the changes without them being removed from the top of the stash.
  • James Meas
    James Meas almost 6 years
    I wrote a c++ program to concatenate the results (I had some 100 objects dangling in my repo, making this necessary). Just compile and run, then pass in your git repo local directory. raw.githubusercontent.com/bluuman/git-recover-files/master/…
  • wberry
    wberry almost 5 years
    Best answer. Easy surgical 'undo' of a single git rm operation without wiping out other uncommitted changes.
  • Reaz Murshed
    Reaz Murshed over 4 years
    git stash; git reset --hard HEAD; git stash pop Did the trick for me! Thanks!
  • Akram
    Akram over 4 years
    Helped me out! best answer.
  • Christian Rolle
    Christian Rolle over 3 years
    This doesn't help in any way.
  • Hmerman6006
    Hmerman6006 over 3 years
    git checkout HEAD . recovered all my folders and files.
  • ollydbg23
    ollydbg23 about 3 years
    This is a good method, after running the git prune -n, I got many lines, those lines are either tree or blob or commit, and I fill out all the hash string of blob, and I try to use the second command to git cat-file -p <blob #>, and I still can't find the lost file. My situation is quite simple, I have create a new file named a.cpp, and I add this file to the index, and later I use the git rm -f to remove this file, and I see that a.cpp get lost.
  • ollydbg23
    ollydbg23 about 3 years
    Sorry about my previous comment, I tested again, it can recover the git rm -f deleted file, I'm not sure why the previous test does not works, thanks!
  • Anagha
    Anagha about 2 years
    This solution does not work. Popping the stash back leads to untracked but still deleted files and folders. The following two options work -- (1) git stash; git reset HEAD; git stash drop 0 (here you are stashing the deleted files/folders and resetting to head and then dropping your stash with deleted changes) (2) git checkout HEAD .