我删除了一些文件。

我还没有承诺。

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

我做了一个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签出{SHA1的版本提交恢复}"{路径文件恢复}"

例如git校验5a6b3179e58edff9c90326b9a04284b02fd67bd0 "src-ui/views/includes/radar.pug"

(在我们想要文件进入的分支中执行)

该命令执行后,恢复的文件将存在于原始位置(需要提交)

其他回答

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

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

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

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

$ git reset HEAD . 

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

$ git checkout .

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

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

下面是在我的mac上帮助我的命令。我尝试了其他一些解决方案,但它们对我不起作用。

OSX Mavericks上的Git版本

mac-pro:main chris$ git version
git version 1.8.5.2 (Apple Git-48)

命令

git checkout HEAD -- path/to/file/file.cc

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 -- <file>

您可以使用下面的命令获得工作树中所有已删除文件的列表。

$ git ls-files --deleted

如果已提交删除,则查找发生删除的提交位置,然后从该提交中恢复文件。

#find the commit hash where it had this file deleted
$ git rev-list -n 1 HEAD -- <file>

它会给你一些类似c46e81aa403ecb8a0f7a323a358068345的东西,现在使用这个提交哈希和父操作符(^),如下所示:

$ git checkout <commit>^ -- <file>

例子:

$ git checkout c46e81aa403ecb8a0f7a323a358068345^ -- <file> 

如果您正在寻找要恢复的文件的路径,下面的命令将显示所有已删除文件的摘要。

$ git log --diff-filter=D --summary

如果你只想显示文件列表:

git log --diff-filter=D --summary | grep "delete mode"