stMind

about Tech, Computer vision and Machine learning

gitのno branchについて

bundleをgit cloneした直後などになる状態。どういう意味だろう?と思って調べてみた。

【git初心者です】カレントブランチが(no branch)になっているんですがこれは何を... - Yahoo!知恵袋

によると、

detached HEAD state。HEADが名前によって参照されていない状態。

.gitを覗いてみる

比較のために、no branchじゃない状態の.gitを覗いてみる。

$ git log --graph --pretty=oneline --decorate --date=short --abbrev-commit --branches
* 05bc8e6 (HEAD, origin/master, master) add alias for ls-remote
* e9eba48 add alias for bundle create
* b6a5232 setting for hadoop and mahout
(長いので省略)
$ less .git/HEAD
ref: refs/heads/master
$ less .git/refs/heads/master
05bc8e65be8c73fb1395dc545488242cb501f3b7

HEADがmasterブランチのリファレンス(refs/heads/master)を指していて、masterブランチのリファレンスは
05bc8e6のコミットを指している。

で、no branchの状態にあるときに.gitの中身を覗いてみる。
git bundle createしてから、git cloneした後。

$ git branch
* (no branch)
$ git log --graph --pretty=oneline --decorate --date=short --abbrev-commit --branches
* 05bc8e6 (HEAD) add alias for ls-remote
* e9eba48 add alias for bundle create
* b6a5232 setting for hadoop and mahout
(長いので省略)
$ less .git/HEAD
05bc8e65be8c73fb1395dc545488242cb501f3b7

確かに、HEADが指しているブランチのリファレンスがない。

なるほど。ということは、ブランチがないコミットのところにチェックアウトしてみてもno branchになりそうだなと。
masterブランチがある方でためしてみると、

$ git checkout b6a5232
Note: checking out 'b6a5232'.

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 b6a5232... setting for hadoop and mahout
$ git branch
* (no branch)
  master

'detached HEAD' stateですよと。確かにno branchにもなってます。
なるほどなるほど。