我删除了一些文件。

我还没有承诺。

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

我做了一个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?


当前回答

只需将git checkout path/to/file-I-want-to-bring-back.txt

其他回答

你已经进行了删除,所以你需要做:

git checkout HEAD cc.properties store/README store/cc.properties

Git结帐。仅从已经执行删除操作的索引中检出。

在寻找如何从另一个分支合并后在我的工作目录中删除文件的答案时发现了这篇文章。合并后尚未作出任何承诺。 因为它是一个正在进行的合并,我不能只是添加它回来使用:

$ git reset <commitid#-where-file.cpp-existed> file.cpp

我不得不做另一个步骤,除了重置文件回来:

$ git checkout -- file.cpp

更新的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一样。不要再搜索了!

如果您正在寻找已删除的目录。

 git checkout ./pathToDir/*

一个没有任何风险的解决方案是转到你的存储库页面(在github等),手动下载已删除的文件。