我删除了一些文件。

我还没有承诺。

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

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

如果它是一个文件夹,只需执行:

git checkout HEAD <foldername>/

参考: https://www.git-tower.com/learn/git/faq/restoring-deleted-files

其他回答

如果你想一次性恢复所有文件

记住使用句点,因为它告诉git抓取所有的文件。

这个命令将重置头部并取消所有的更改:

$ git reset HEAD . 

然后运行该命令恢复所有文件:

$ git checkout .

然后做一个git状态,你会得到:

$ git status
On branch master
Your branch is up-to-date with 'origin/master'.

我也遇到了同样的问题,但上面的解决方案都不适合我。 我最后做的是: —创建同名的空文件 -将该文件与本地历史文件进行比较 -将历史记录复制到空文件。

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

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

 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.