我删除了一些文件。

我还没有承诺。

我想重置我的工作空间来恢复文件。

我做了一个git结帐。

但被删除的文件还是不见了。

git状态显示:

# On branch master
# Changes to be committed:
#   (use "git reset HEAD <file>..." to unstage)
#
#   deleted:    cc.properties
#   deleted:    store/README
#   deleted:    store/cc.properties
#

为什么不结帐。将工作空间重置为HEAD?


当前回答

1.找到你想要恢复使用的特定提交:

   git log
This command will give you a list of commits done by you .

2.恢复到该提交使用:

    git revert <commit id> 

现在您的本地分支将拥有所有特定的文件

其他回答

如果你用过

git rm filename

然后删除一个文件

git checkout path/to/filename

不管用,在这种情况下

git checkout HEAD^ path/to/filename

应该工作

输出告诉您需要做什么。git重置HEAD cc.properties等。

这将取消rm操作。在此之后,再次运行git状态会告诉您需要执行git checkout——cc.properties来取回文件。

更新: 我的配置文件中有这个

$ git config alias.unstage
reset HEAD

我通常用它来取消舞台上的东西

你想看这个吗

这适用于你用过的情况

git checkout -- .

在你做出承诺之前。

您可能还希望删除尚未创建的已创建文件。你不想要他们。:

git reset -- .

自动地一次性恢复所有未分级的删除,而不指定每一个路径:

git ls-files -z -d | xargs -0 git checkout --

自动地一次性恢复所有的阶段性删除,而不指定每一个路径:

git status | grep 'deleted:' | awk '{print $2}' | xargs git checkout --

更新的git(我的是2.27.0)更友好,实际的命令会在“git状态”中显示。 例如,如果您删除了文件tf.c,那么

$ git status
...
Changes not staged for commit:
  (use "git add/rm <file>..." to update what will be committed)
  (use "git restore <file>..." to discard changes in working directory)
      deleted:    tf.c

你可以使用“git restore tf.c”来恢复它,就像它saz一样。不要再搜索了!