这种情况工作中遇见过,用repo版本库拉下来的时候有些git仓会出现git branch的时候会出现[detached HEAD...的标识],意思是,处于游离的状态,不属于任何分支,如果这种状态提交,git push会报错,因为没有分支。我们看看为什么会出现这种情况
前面说过HEAD ref/heads/master master指向的是同一个最新提交 即表示当前版本库HEAD指向为master
jagent@ubuntu:~/xin/mytest$ cat .git/refs/heads/master
6c8826b6580eb82344b087f4ba3a76893ca5919c
jagent@ubuntu:~/xin/mytest$ git branch -vv
* master 6c8826b [origin/master] TicketNo:DTS2020062200775 Description:ci test Team:ETCD1_CI Feature or Bugfix:Feature Binary Source:No PrivateCode(Yes/No):No
如果git checkout命令检出ID的父提交,看会出现什么情况
jagent@ubuntu:~/xin/mytest$ git log --oneline
6c8826b TicketNo:DTS2020062200775 Description:ci test Team:ETCD1_CI Feature or Bugfix:Feature Binary Source:No PrivateCode(Yes/No):No
989c771 1
4302e9b TicketNo:DTS2020062200775 Description:ci test Team:ETCD1_CI Feature or Bugfix:Feature Binary Source:No PrivateCode(Yes/No):No
fa2e467 patch 3
9f574c8 patch 2
3d4d66d patch 1
7c4368e begin
jagent@ubuntu:~/xin/mytest$ git checkout 989c771
Note: checking out '989c771'.
You are in 'detached HEAD' state. You can look around, make experimental
changes and commit them, and you can discard any commits you make in this
state without impacting any branches by performing another checkout.
If you want to create a new branch to retain commits you create, you may
do so (now or later) by using -b with the checkout command again. Example:
git checkout -b new_branch_name
HEAD is now at 989c771... 1
git已经再提示我们了将会出现detached HEAD状态
1.我们可以通过git reflog -1来查看
jagent@ubuntu:~/xin/mytest$ git reflog -1
989c771 HEAD@{0}: checkout: moving from master to 989c771
2.或者通过查看HEAD和master对应的提交ID
jagent@ubuntu:~/xin/mytest$ git rev-parse HEAD master
989c771084fad763d3b0cad8848ef79199b6c439
6c8826b6580eb82344b087f4ba3a76893ca5919c
从这可以看出当前HEAD指向的不是master分支了而是具体的提交了,简单说下master其实是最新提交的引用即6c8826b6580eb82344b087f4ba3a76893ca5919c
再这种状态提交push试试呢
jagent@ubuntu:~/xin/mytest$ touch test.txt
jagent@ubuntu:~/xin/mytest$ git add .
jagent@ubuntu:~/xin/mytest$ git commit
[detached HEAD 0c982c4] TicketNo:DTS2020062200775 Description:ci test Team:ETCD1_CI Feature or Bugfix:Feature Binary Source:No PrivateCode(Yes/No):No
1 file changed, 0 insertions(+), 0 deletions(-)
create mode 100644 test.txt
jagent@ubuntu:~/xin/mytest$ git push origin HEAD --no-thin
error: unable to push to unqualified destination: HEAD
The destination refspec neither matches an existing ref on the remote nor
begins with refs/, and we are unable to guess a prefix based on the source ref.
error: failed to push some refs to 'ssh://xwx496887@10.148.192.216:29418/ST/mytest'
报错了,因为是游离状态,不属于任何分支,所以是推送不上去的
2.如果我们切换分支呢
git checkout master
jagent@ubuntu:~/xin/mytest$ git checkout master
Warning: you are leaving 1 commit behind, not connected to
any of your branches:
0c982c4 TicketNo:DTS2020062200775 Description:ci test Team:ETCD1_CI Feature or Bugfix:Feature Binary Source:No PrivateCode(Yes/No):No
If you want to keep them by creating a new branch, this may be a good time
to do so with:
git branch new_branch_name 0c982c4
Switched to branch 'master'
Your branch is up-to-date with 'origin/master'.
git会给出警告,刚刚我们刚刚在detached HEAD状态的提交会消失了。
git log --oneline
jagent@ubuntu:~/xin/mytest$ git log --oneline
6c8826b TicketNo:DTS2020062200775 Description:ci test Team:ETCD1_CI Feature or Bugfix:Feature Binary Source:No PrivateCode(Yes/No):No
989c771 1
4302e9b TicketNo:DTS2020062200775 Description:ci test Team:ETCD1_CI Feature or Bugfix:Feature Binary Source:No PrivateCode(Yes/No):No
fa2e467 patch 3
9f574c8 patch 2
3d4d66d patch 1
7c4368e begin
那刚刚的提交会消失吗(爱不会消失,只会转移),其实他还存在版本库中通过提交的commitid取查找
jagent@ubuntu:~/xin/mytest$ git show 0c982c4
commit 0c982c4aa33af9eb9062c27e7a215f92ab3beae4
Author: xwx496887 <xwx496887@notesmail.huawei.com>
Date: Fri Nov 6 10:30:34 2020 +0800
TicketNo:DTS2020062200775
Description:ci test
Team:ETCD1_CI
Feature or Bugfix:Feature
Binary Source:No
PrivateCode(Yes/No):No
new file: test.txt
diff --git a/test.txt b/test.txt
new file mode 100644
index 0000000..e69de29
如果这个提交需要放入master分支,可以用命令git merge或者git rebase(后续介绍他们的区别)
git merge
jagent@ubuntu:~/xin/mytest$ git merge 0c982c4
Merge made by the 'recursive' strategy.
test.txt | 0
1 file changed, 0 insertions(+), 0 deletions(-)
create mode 100644 test.txt
查看git log 发现已经出现在版本库中了
jagent@ubuntu:~/xin/mytest$ git log --oneline
afcc4e7 Merge commit '0c982c4'
0c982c4 TicketNo:DTS2020062200775 Description:ci test Team:ETCD1_CI Feature or Bugfix:Feature Binary Source:No PrivateCode(Yes/No):No
6c8826b TicketNo:DTS2020062200775 Description:ci test Team:ETCD1_CI Feature or Bugfix:Feature Binary Source:No PrivateCode(Yes/No):No
989c771 1
4302e9b TicketNo:DTS2020062200775 Description:ci test Team:ETCD1_CI Feature or Bugfix:Feature Binary Source:No PrivateCode(Yes/No):No
fa2e467 patch 3
9f574c8 patch 2
3d4d66d patch 1
7c4368e begin
值得注意的是,这些都是本地仓库做的事情
显然,这个提交也会过期的,因为他不属于任何分支,属于黑户,当reflog日志过期后,就会从版本库中删除