Git / 分离 HEAD,恢复工作吗?
我对我认为是我的分支进行了数十次提交,然后检查了另一个分支。
愿意回到我最初的分支,我没有找到我更新的代码。在控制台中查看我的历史记录后,我了解到我在一个独立分支中工作......
是否有可能以某种方式获得我在独立分支上完成的工作?
I made a dozens of commits on what I thought was my branch, then checked out another branch.
Willing to go back to my initial branch, I didn't find my updated code. After looking at my history in console, I understood I worked in a detached branch...
Is it somehow possible to get the job I've done on the detached branch?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
是的。您可以使用重新记录。尝试 git log -g HEAD 。这将向您显示
HEAD
的引用日志,即HEAD
指向的每个提交,以及它更改为该提交的原因。您应该能够找到签出分支的命令,并查看之前的提交是什么。您还可以使用其他语法来索引引用日志。如果您刚刚执行了 git checkout 分支,则
HEAD@{1}
将引用之前签出的提交(因此您可以 git checkout HEAD@{ 1} 返回到它)。或者,如果您知道 10 分钟前HEAD
指向正确的内容,则可以使用git checkout HEAD@{10.minutes.ago}
。Yes. You can use the reflog. Try
git log -g HEAD
. This will show you the reflog forHEAD
, i.e. every single commit thatHEAD
has pointed to, and the reason why it changed to that commit. You should be able to find your command that checked out the branch, and see what the previous commit was.You can also use other syntax to index into the reflog. If you just performed the
git checkout branch
, thenHEAD@{1}
will refer to the previous checked-out commit (so you cangit checkout HEAD@{1}
to get back to it). Or if you know that 10 minutes agoHEAD
was pointing to the right thing, you can usegit checkout HEAD@{10.minutes.ago}
.放松,一切都还在那里 :)
只需调用
git
就会告诉您HEAD
之前指向的提交。将会有一行像用独立的头告诉您提交尖端的 SHA1 一样。使用现在创建一个指向该提示的新分支,
您可以在提交应该转到的分支之上对该分支进行变基。
Relax, everything is still there :)
Just call
and
git
will tell you to what commitsHEAD
pointed before. There will be a line liketelling you the SHA1 of the tip of your commits with a detached head. Create a new branch pointing to that tip using
Now you can rebase that branch on top of the branch the commits were supposed to go to.