我删除了一些文件。

我还没有承诺。

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

我做了一个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 ./pathToDir/*

其他回答

I had the same problem and none of the answers here I tried worked for me either. I am using Intellij and I had checked out a new branch git checkout -b minimalExample to create a "minimal example" on the new branch of some issue by deleting a bunch of files and modifying a bunch of others in the project. Unfortunately, even though I didn't commit any of the changes on the new "minimal example" branch, when I checked out my "original" branch again all of the changes and deletions from the "minimal example" branch had happened in the "original" branch too (or so it appeared). According to git status the deleted files were just gone from both branches.

Fortunately, even though Intellij had warned me "deleting these files may not be fully recoverable", I was able to restore them (on the minimal example branch from which they had actually been deleted) by right-clicking on the project and selecting Local History > Show History (and then Restore on the most recent history item I wanted). After Intellij restored the files in the "minimal example" branch, I pushed the branch to origin. Then I switched back to my "original" local branch and ran git pull origin minimalExample to get them back in the "original" branch too.

您可以使用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将恢复两者。

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

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

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

更新的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 path/to/file-I-want-to-bring-back.txt