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


当前回答

对于那些丢失了未分期和未提交更改的VSCode用户

这只会工作,如果你没有广泛编辑您的文件后再次重置。

尝试在VSCode中撤销文件更改

我确实进行了重置——当我的利益相关者决定在我构建功能时重新定义功能时,这很困难。一小时后,同样的利益相关者改变了主意。

我休息了一会儿又回来了。当我看到他们改变主意后,我决定尝试撤销我正在处理的文件,它成功了!我设法拿回了所有的作业。

其他回答

根据定义,git reset -hard会丢弃未提交的更改,git无法恢复它们(你的备份系统可能会有帮助,但不是git)。

实际上,很少有git重置的情况——困难是个好主意。在大多数情况下,有一个更安全的命令来做同样的事情:

If you want to throw away your uncommitted changes, then use git stash. It will keep a backup of these changes, which will expire after some time if you run git gc. If you're 99.9% sure you'll never need these changes back, then git stash is still your friend for the 0.1% case. If you're 100% sure, then git stash is still your friend because these 100% have a measurement error ;-). If you want to move your HEAD and the tip of the current branch in history, then git reset --keep is your friend. It will do the same thing as git reset --hard, but will not discard your local changes. If you want to do both, then git stash && git reset --keep is your friend.

教你的手指不要使用git重置——很难,总有一天会有回报的。

一般来说,您无法返回未提交的更改。

以前的分段更改(git add)应该可以从索引对象中恢复,所以如果你这样做了,使用git fsck——lost-found来定位与它相关的对象。(这将对象写到.git/lost-found/目录;从那里你可以使用git show <filename>来查看每个文件的内容。)

如果没有,答案是:看看你的备份。也许你的编辑器/IDE将临时拷贝存储在/tmp或C:\ temp之类的目录下

git reset HEAD@{1}

这将恢复到前一个HEAD

[1] vim例如,可选地存储持久撤销,eclipse IDE存储本地历史;这样的功能可能会节省你的**

我也遇到了同样的问题,我几乎要疯了....最初我提交了项目并合并。 后来当我尝试运行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

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

当我在做一个本地项目时,我想把它移动到GitHub上,然后创建一个新的存储库。当我试图用.gitignore将所有这些文件添加到新的存储库中时,我不小心添加了一个错误的文件,然后试图清除它。

我运行了git reset -hard origin/master

然后我所有的本地文件都被删除了,因为回购是空的。我以为一切都消失了。

这招对我很管用:

git reflog show
git reset HEAD@{1} 
git push