Git / 分离 HEAD,恢复工作吗?

发布于 2025-01-06 05:13:35 字数 139 浏览 1 评论 0原文

我对我认为是我的分支进行了数十次提交,然后检查了另一个分支。

愿意回到我最初的分支,我没有找到我更新的代码。在控制台中查看我的历史记录后,我了解到我在一个独立分支中工作......

是否有可能以某种方式获得我在独立分支上完成的工作?

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

冬天的雪花 2025-01-13 05:13:35

是的。您可以使用重新记录。尝试 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 for HEAD, i.e. every single commit that HEAD 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, then HEAD@{1} will refer to the previous checked-out commit (so you can git checkout HEAD@{1} to get back to it). Or if you know that 10 minutes ago HEAD was pointing to the right thing, you can use git checkout HEAD@{10.minutes.ago}.

七七 2025-01-13 05:13:35

放松,一切都还在那里 :)

只需调用

git reflog

git 就会告诉您 HEAD 之前指向的提交。将会有一行像

checkout: moving from c70e36e25ac2dbedde6cb376719381fe0ab53f19 to master

用独立的头告诉您提交尖端的 SHA1 一样。使用现在创建一个指向该提示的新分支,

git branch saved-commits c70e36e25ac2dbedde6cb376719381fe0ab53f19

您可以在提交应该转到的分支之上对该分支进行变基。

Relax, everything is still there :)

Just call

git reflog

and git will tell you to what commits HEAD pointed before. There will be a line like

checkout: moving from c70e36e25ac2dbedde6cb376719381fe0ab53f19 to master

telling you the SHA1 of the tip of your commits with a detached head. Create a new branch pointing to that tip using

git branch saved-commits c70e36e25ac2dbedde6cb376719381fe0ab53f19

Now you can rebase that branch on top of the branch the commits were supposed to go to.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文