我看到几乎每个人都提出了一个正在创建临时分支的解决方案。现在,需要承认,每当出现这种“以分离状态提交”的问题时,通常会在一次提交后检测到。并为那个微不足道的提交创建一个完整的分支 - 听起来太多了,对吧?尤其是在你已经在太多分支之间跳来跳去的项目中。
那有什么简单的方法呢?使用提交哈希!
我怎么得到它?
- 做一个
git log
。你会看到这样的东西:
commit 10bf8fe4d17bb7de59586a7abb6db321f0786bb3 (HEAD)
Author: Someone <someone@something.com>
Date: So/me/day SO:ME:TI:ME
A commit message that doesn't mean much
commit a3cd1cedf1962916cdc2945f2bd2b271ec8b919d (origin/master, master)
Author: Someone <someone@something.com>
Date: Some/other/day SOME:OTHER:TIME
Another commit message that doesn't mean much
commit 1bfabbe09c70419070fe29ff1ed276c0207bbe10
Author: Someone <someone@something.com>
Date: Thu Jul 8 08:38:12 2021 +0530
Enough reading the example, focus on the answer!!
现在,虽然它看起来像一个正常的情况,但是当你这样做时,git push
它会说“一切都是最新的”。
细心的人会发现它不是“最新的”。HEAD
在主人以外的地方。
- 那么,接下来呢?只需复制 hash 的初始字符即可
10bf8fe4d1
。首先,做一个git checkout master
. 这会将您转移到master
分支。现在因为你已经复制了哈希。你可以做一个git merge <hash>
. git log
现在做
瞧:
commit 10bf8fe4d17bb7de59586a7abb6db321f0786bb3 (HEAD -> master)
Author: Someone <someone@something.com>
Date: S/om/eday SO:ME:TI:ME
A commit message that doesn't mean much
commit a3cd1cedf1962916cdc2945f2bd2b271ec8b919d (origin/master)
Author: Someone <someone@something.com>
Date: Some/other/day SOME:OTHER:TIME
Another commit message that doesn't mean much
commit 1bfabbe09c70419070fe29ff1ed276c0207bbe10
Author: Someone <someone@something.com>
Date: Thu Jul 8 08:38:12 2021 +0530
Enough reading the example, focus on the answer!!
现在,这HEAD
似乎是在一个适当的地方。
有人可能会问,“如果我没有哈希怎么办?我对悬空提交一无所知,只是做了一个git checkout master
.” 别担心,我帮你搞定了。您可以在两个地方找到提交哈希:
- 当你这样做时
git checkout master
,git
曾这样警告过你
Warning: you are leaving 1 commit behind, not connected to
any of your branches:
10bf8fe A commit message that doesn't mean much
If you want to keep it by creating a new branch, this may be a good time
to do so with:
git branch <new-branch-name> 10bf8fe
Switched to branch 'master'
你可以看到你的宝藏(hash
),对吧?
- 别告诉我你找不到。它就在那里。但如果你真的不能,那么你可以做一个
git reflog
. 它会向您显示如下内容:
a3cd1ce (HEAD -> master, origin/master) HEAD@{0}: checkout: moving from 10bf8fe4d17bb7de59586a7abb6db321f0786bb3 to master
10bf8fe HEAD@{1}: commit: A commit message that doesn't mean much
你看到那里有你正在寻找的宝藏......哈希。
我想这涵盖了在带有悬空提交的分离状态下可能发生的所有可能场景。下次注意!!