我删除了一些文件。

我还没有承诺。

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

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


当前回答

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

由于您正在执行git签出,看起来您正在尝试将分支恢复到上次提交状态。

你可以通过git重置HEAD来实现这一点——很难

警告

这样做可能会删除所有最新的修改并取消您的修改,例如,您可能会丢失工作。这可能是你想要的,但请检查文件以确保。

注意:先完成你想保留的工作。

您可以重置您的工作区(并恢复已删除的文件)

git checkout ./*

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

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

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

使用git ls-files签出删除(-d)或修改(-m)的文件。

git checkout $(git ls-files -d)

参见如何仅恢复git签出上修改的文件?