如何恢复“git rm -r .”?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/2125710/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-10 07:42:51  来源:igfitidea点击:

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

gitgit-rm

提问by user89021

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

我不经意地说git rm -r .。我该如何从中恢复?

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 rm -r .. 因为我不确定在上次提交之后和git rm -r ..

回答by Brian Campbell

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 stash
git reset --hard HEAD
git stash pop

回答by Jaime Bellmyer

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-rm 处理了一些文件并在下一次提交之前继续进行更改。如果需要,您可以简单地检出遗漏/删除的单个文件,而不是隐藏和重置:

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

This leaves your other uncommitted changes intact with no workarounds.

这使您的其他未提交的更改完好无损,没有变通方法。

回答by Arne L.

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/fileand recreate the file as it was in the last commit, i.e.HEAD.

这将首先为path/to/file上次提交重新创建索引条目并重新创建文件,即HEAD.

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

提示:可以将提交哈希传递给两个命令,以从较旧的提交重新创建文件。查看git reset --helpgit checkout --help了解详情。

回答by Alex Brown

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 rm .删除了工作检出以及索引中此目录和子目录中的所有文件,因此您需要撤消这些更改中的每一个:

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 changesyou have made to your files.

会起作用,并且不会删除您对文件所做的任何未提交的更改

after that you need to repeat any git addcommands you had queued up.

之后,您需要重复git add排队的任何命令。

回答by Skippy VonDrake

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

如果您最终没有完成上述工作,您可以使用以下建议检索数据:http: //www.spinics.net/lists/git/msg62499.html

git prune -n
git cat-file -p <blob #>

回答by song xu

undo git rm

撤消 git rm

git rm file             # delete file & update index
git checkout HEAD file  # restore file & index from HEAD

undo git rm -r

撤消 git rm -r

git rm -r dir          # delete tracked files in dir & update index
git checkout HEAD dir  # restore file & index from HEAD


undo git rm -rf

撤消 git rm -rf

git rm -r dir          # delete tracked files & delete uncommitted changes
not possible           # `uncommitted changes` can not be restored.

Uncommitted changesincludes not staged changes, staged changes but not committed.

Uncommitted changes包括not staged changesstaged changes but not committed

回答by Cory Danielson

If you've committed and pushed the changes, you can do this to get the file back

如果您已提交并推送更改,则可以执行此操作以取回文件

// Replace 2 with the # of commits back before the file was deleted.
git checkout HEAD~2 path/to/file

回答by mehtunguh

There are some good answers already, but I might suggest a little-used syntax that not only works great, but is very explicit in what you want (therefor not scary or mysterious)

已经有一些很好的答案,但我可能会建议一个很少使用的语法,它不仅效果很好,而且非常明确你想要什么(因此不可怕或神秘)

git checkout <branch>@{"20 minutes ago"} <filename>

回答by Do Nhu Vy

Get list commit

获取列表提交

git log  --oneline

For example, Stable commit has hash: 45ff319c360cd7bd5442c0fbbe14202d20ccdf81

例如,稳定提交具有哈希值: 45ff319c360cd7bd5442c0fbbe14202d20ccdf81

git reset --hard 45ff319c360cd7bd5442c0fbbe14202d20ccdf81
git push -ff origin master

回答by Artur

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

我有一个相同的情况。在我的情况下,解决方案是:

git checkout -- .