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

我没有承诺。

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

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


当前回答

如果您已经提交并提交了更改,那么您可以这样做来取回文件

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

其他回答

如果您已经提交并提交了更改,那么您可以这样做来取回文件

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

我也遇到过同样的情况。对我来说,解决方案是:

git checkout -- .

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

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

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

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

使用Git 2.23+(2019年8月),恢复文件(和索引)的正确命令是使用…Git恢复(不是重置——很难或令人困惑的Git checkout命令)

那就是:

git restore -s=HEAD --staged --worktree -- .

或其缩写形式:

git restore -s@ -SW -- .

获取列表提交

git log  --oneline

例如,稳定提交的哈希值为:45ff319c360cd7bd5442c0fbbe14202d20ccdf81

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