bash Git / 分离的 HEAD,重新开始工作?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/9321145/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-18 01:35:03  来源:igfitidea点击:

Git / detached HEAD, get work back?

gitbashbranchzsh

提问by apneadiving

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?

是否有可能完成我在独立分支上完成的工作?

回答by Lily Ballard

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 HEADhas 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.

是的。您可以使用引用日志。试试git log -g HEAD。这将显示 reflog HEAD,即HEAD指向的每个提交,以及它更改为该提交的原因。您应该能够找到检出分支的命令,并查看之前的提交是什么。

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 HEADwas pointing to the right thing, you can use git checkout HEAD@{10.minutes.ago}.

您还可以使用其他语法来索引引用日志。如果您刚刚执行了git checkout branch,HEAD@{1}则将参考之前签出的提交(以便您可以git checkout HEAD@{1}返回)。或者,如果您知道 10 分钟前HEAD指向正确的内容,则可以使用git checkout HEAD@{10.minutes.ago}.

回答by Sven Marnach

Relax, everything is still there :)

放松,一切都还在:)

Just call

打电话就行

git reflog

and gitwill tell you to what commits HEADpointed before. There will be a line like

并且git会告诉你HEAD之前提交的内容。会有一条线

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

用分离的头部告诉你提交尖端的 SHA1。使用创建一个指向该提示的新分支

git branch saved-commits c70e36e25ac2dbedde6cb376719381fe0ab53f19

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

现在,您可以在提交应该转到的分支之上重新设置该分支。