我删除了一些文件。

我还没有承诺。

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

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


当前回答

我碰巧将一些json文件从一个文件夹移动(而不是复制)到同一存储库中的另一个文件夹。然后我重命名了这些文件,并在新位置更改了一些内容。然而,我很快了解到,我没有复制和完全删除文件从以前的位置。

简单的解决方案:

git reset HEAD <oldfilepath_which_was_moved>
git restore <oldfilepath_which_was_moved>

对所有文件都这么做了,它们都回来了。

您还可以包含多个文件,以空格分隔。

git reset HEAD file_1_path file_2_path file_3_path

很容易修复,顺便说一下,这不会改变/删除新文件。

其他回答

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

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

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

输出告诉您需要做什么。git重置HEAD cc.properties等。

这将取消rm操作。在此之后,再次运行git状态会告诉您需要执行git checkout——cc.properties来取回文件。

更新: 我的配置文件中有这个

$ git config alias.unstage
reset HEAD

我通常用它来取消舞台上的东西

我碰巧将一些json文件从一个文件夹移动(而不是复制)到同一存储库中的另一个文件夹。然后我重命名了这些文件,并在新位置更改了一些内容。然而,我很快了解到,我没有复制和完全删除文件从以前的位置。

简单的解决方案:

git reset HEAD <oldfilepath_which_was_moved>
git restore <oldfilepath_which_was_moved>

对所有文件都这么做了,它们都回来了。

您还可以包含多个文件,以空格分隔。

git reset HEAD file_1_path file_2_path file_3_path

很容易修复,顺便说一下,这不会改变/删除新文件。

1.找到你想要恢复使用的特定提交:

   git log
This command will give you a list of commits done by you .

2.恢复到该提交使用:

    git revert <commit id> 

现在您的本地分支将拥有所有特定的文件

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.