我看到每个人,几乎每个人都提出了一个临时分支正在创建的解决方案。现在,人们需要承认,无论何时出现这种“在分离状态下提交”的问题,通常都是在一次提交后检测到的。并为一个微不足道的提交创建一个完整的分支-听起来太多了,对吗?特别是在您已经在太多分支之间跳跃的项目中。
那么有什么简单的方法呢?使用提交哈希!
我怎么得到它?
做一个git日志。你会看到这样的东西:
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时,它会说"Everything - updated "
细心的人会发现它不是“最新的”。HEAD不是master。
那么,接下来呢?只需复制散列10bf8fe4d1的初始字符。首先,执行git checkout master。这将把你移到主分支。现在既然你已经复制了哈希值。你可以做git merge <hash>。现在做git日志
瞧:
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'
你可以看到你的宝藏(哈希),对吗?
别告诉我你找不到。就在那儿。但如果你真的不能,你可以做一个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
你看,这就是你一直在寻找的宝藏……散列。
我想这涵盖了悬垂提交的分离状态下可能发生的所有可能情况。下次当心!!