有没有办法从git重置中恢复未提交的更改到工作目录-hard HEAD?


当前回答

答案来自这个SO

$ git reflog show

4b6cf8e (HEAD -> master, origin/master, origin/HEAD) HEAD@{0}: reset: moving to origin/master
295f07d HEAD@{1}: pull: Merge made by the 'recursive' strategy.
7c49ec7 HEAD@{2}: commit: restore dependencies to the User model
fa57f59 HEAD@{3}: commit: restore dependencies to the Profile model
3431936 HEAD@{4}: commit (amend): restore admin
033f5c0 HEAD@{5}: commit: restore admin
ecd2c1d HEAD@{6}: commit: re-enable settings app

# assuming you want to get back to 7c49ec7 (restore dependencies to the User model)

$ git reset HEAD@{2}

你的好日子回来了!:)

其他回答

(适用于部分用户的答案)

如果你使用的是(最近的)macOS,即使你没有使用时间机器磁盘,操作系统也会每小时保存一次备份,称为 当地的快照。

进入时间机器,导航到你丢失的文件。操作系统会问你:

The location to which you're restoring "file.ext" already contains an
item with the same name. Do you want to replace it with the one you're
restoring?

你应该能够恢复你丢失的文件。

我发现在git重置之前任何未提交的文件——hard <commit>会从git历史中删除。然而,我很幸运,在我紧张的整个过程中,我一直保持我的代码编辑器会话打开,我发现在每个受影响的文件中,一个简单的控件+ z将文件的状态返回到Git重置之前的版本,所以我没有特别要求它重置所有内容。万岁! !

我遇到了这样一个场景,我签出了本地更改,但没有提交或存储它们。据我所知,没有任何命令能帮助我。对我有用的是CTRL+Z。

CTRL + Z

偶然的机会,我所有的文件都在编辑器中打开,所以在编辑器中,我只需按CTRL+Z所需的文件,这解决了我的问题

PS:我用的是Sublime文本编辑器。

嗯,据我所知,最好的解决方案是使用IDE特性。

选择丢失文件的存储库,并右键单击它 在菜单>中查找本地历史显示历史 现在检查版本版本,你所有的文件都存在>,选择它,然后点击恢复。 这就是最可能的情况。

注意:上述解决方案适用于在任何IDE(如intelliJ)中进行更改或导入存储库。

我也遇到了同样的问题,我几乎要疯了....最初我提交了项目并合并。 后来当我尝试运行git push——set-upstream origin master时,我得到了这个错误

致命的:拒绝合并不相关的历史

所以我运行git reset—hard HEAD,它删除了一个3周的项目,但下面的几个命令挽救了这一天:

git reset HEAD@{1}         //this command unstage changes after reset
git fsck --lost-found      //I got the dangling commit fc3b6bee2bca5d8a7e16b6adaca6a76e620eca4b
git show <dangling commit something like-> fc3b6bee2bca5d8a7e16b6adaca6a76e620eca4b>
git rebase fc3b6bee2bca5d8a7e16b6adaca6a76e620eca4b