我不小心说了git rm -r ..我该如何从中恢复呢?

我没有承诺。

我认为所有的文件都被标记为删除,并从我的本地签出中物理删除。

编辑:我可以(如果我知道命令)恢复到上次提交。但如果我能撤销git rm -r ..因为我真的不确定我在最后一次提交之后和git rm -r之前做了什么。


当前回答

要重新获得一些单个文件或文件夹,可以使用以下方法

git reset -- path/to/file
git checkout -- path/to/file

这将首先重新创建路径/to/file的索引项,并重新创建上次提交时的文件,即head。

提示:可以向两个命令都传递一个提交哈希来从旧的提交中重新创建文件。详见git reset -help和git checkout -help。

其他回答

git reset HEAD

应该这么做。如果您不关心任何未提交的更改,那么

git reset --hard HEAD

应该强制重置您上次提交的所有内容。如果你有未提交的更改,但是第一个命令不起作用,那么用git stash保存你未提交的更改:

git stash
git reset --hard HEAD
git stash pop

如果执行命令git rm -r——cached . xml。在一个没有进行暂存(也就是没有提交)的repo中,你可以使用命令git restore——staging来撤销这个操作(从删除中取消暂存)。

简而言之,撤销git rm -r——cached。你只需要运行git restore - staging。

更新:

因为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命令。

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