我删除了一些文件。

我还没有承诺。

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

我做了一个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(我的是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 -- .

在你做出承诺之前。

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

git reset -- .

这对我来说是最简单的方法:

git checkout HEAD~1 path/to/myfile.rb

我在这里找到的。


另一种方法也适用于我:

git reset HEAD path/to/myfile.rb
git restore path/to/myfile.rb
git checkout HEAD -- client/src/pp_web/index.cljs

如果您已经安装ToroiseGIT,那么只需选择“恢复…”菜单项的父文件夹弹出菜单。

您可以使用git-restore一次性恢复文件。

如果你没有像这样提交你的删除:

➜  learning git:(daily) ✗ git status
Changes to be committed:
  (use "git restore --staged <file>..." to unstage)
    deleted:    feelings/articles/2022/1/assets/20220109_093659_image.png
    deleted:    feelings/articles/2022/1/assets/20220109_094525_image.png
    deleted:    feelings/articles/2022/1/assets/20220109_100231_image.png
    deleted:    feelings/articles/2022/1/assets/20220109_100251_image.png
    deleted:    feelings/articles/2022/1/assets/20220109_100321_image.png
    deleted:    feelings/articles/2022/1/assets/20220109_101009_image.png

你可以使用这样的方法来恢复你的文件:

git restore --worktree --staged feelings/articles/2022/1/assets

-W,——worktree, -S,——staging 指定恢复位置。如果没有指定任何选项,默认情况下将恢复工作树。指定——staging只会恢复索引。 指定both将恢复两者。