我不小心说了git rm -r ..我该如何从中恢复呢?
我没有承诺。
我认为所有的文件都被标记为删除,并从我的本地签出中物理删除。
编辑:我可以(如果我知道命令)恢复到上次提交。但如果我能撤销git rm -r ..因为我真的不确定我在最后一次提交之后和git rm -r之前做了什么。
我不小心说了git rm -r ..我该如何从中恢复呢?
我没有承诺。
我认为所有的文件都被标记为删除,并从我的本地签出中物理删除。
编辑:我可以(如果我知道命令)恢复到上次提交。但如果我能撤销git rm -r ..因为我真的不确定我在最后一次提交之后和git rm -r之前做了什么。
当前回答
已经有了一些很好的答案,但我可能会建议一个很少使用的语法,它不仅工作得很好,而且非常明确地表达了您想要的东西(因此并不可怕或神秘)。
git checkout <branch>@{"20 minutes ago"} <filename>
其他回答
撤销git rm
git rm file # delete file & update index
git checkout HEAD file # restore file & index from HEAD
撤销git rm -r
git rm -r dir # delete tracked files in dir & update index
git checkout HEAD dir # restore file & index from HEAD
撤销git rm -rf
git rm -r dir # delete tracked files & delete uncommitted changes
not possible # `uncommitted changes` can not be restored.
未提交的更改包括非阶段性更改,阶段性更改但未提交。
我也遇到过同样的情况。对我来说,解决方案是:
git checkout -- .
如果您已经提交并提交了更改,那么您可以这样做来取回文件
// Replace 2 with the # of commits back before the file was deleted.
git checkout HEAD~2 path/to/file
更新:
因为git rm。删除工作签出中的this和子目录以及索引中的所有文件,你需要撤消这些更改:
git reset HEAD . # This undoes the index changes
git checkout . # This checks out files in this and child directories from the HEAD
这应该是你想要的。它不会影响签出代码或索引的父文件夹。
以前的答案不是:
reset HEAD
将会做到这一点,并且不会删除您对文件所做的任何未提交的更改。
在此之后,您需要重复您已经排队的所有git add命令。
如果上述方法都不能正常工作,则可以使用这里的建议检索数据:http://www.spinics.net/lists/git/msg62499.html
git prune -n
git cat-file -p <blob #>