How to revert a “git rm -r .”?

2019-01-03 07:41发布

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 git-rm
11条回答
兄弟一词,经得起流年.
2楼-- · 2019-01-03 08:10

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.

查看更多
虎瘦雄心在
3楼-- · 2019-01-03 08:13

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.

查看更多
Fickle 薄情
4楼-- · 2019-01-03 08:18

I had an identical situation. In my case the solution was:

git checkout -- .
查看更多
虎瘦雄心在
5楼-- · 2019-01-03 08:21

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 #>
查看更多
ゆ 、 Hurt°
6楼-- · 2019-01-03 08:22

I had exactly the same issue: was cleaning up my folders, rearranging and moving files. I entered: git rm . and hit enter; and then felt my bowels loosen a bit. Luckily, I didn't type in git commit -m "" straightaway.

However, the following command

git checkout .

restored everything, and saved my life.

查看更多
登录 后发表回答