git log --decorate: (HEAD -> master) vs (HEAD, master) 的输出差异

Difference in the output of git log --decorate: (HEAD -> master) vs (HEAD, master)

当我获得 GIT 存储库的日志时:

git log --oneline --decorate --graph

输出是这样的:

* 44025ed (HEAD -> master) second commit
* adf2dbb first commmit

在另一个 repo 中,当我 git log 时,我得到:

* 435b61d (HEAD,master) bar
* 9773e52 foo

(HEAD -> master)(HEAD,master)有什么区别

箭头指向当前分支

HEAD 右侧的箭头,在 git log --oneline --decorate --graph 的输出中,指示哪个分支(如果有)是当前分支。

* 44025ed (HEAD -> master) second commit

表示符号引用HEAD当前指向master分支;换句话说,你 not 处于 detached-HEAD 状态,当前分支是 master.

相比之下,

* 44025ed (HEAD, master) second commit

表示符号引用HEAD 不是当前指向任何分支,而是直接指向提交(44025ed);换句话说,你处于 detached-HEAD 状态。 master 分支仅与 HEAD 一起列出,因为它恰好指向同一个提交 (44025ed)。

一些历史

作为参考,在我提出以下问题后不久,Git (2.4) 引入了这种区别:Can git log --decorate unambiguously tell me whether the HEAD is detached?

一个小实验(修正思路)

$ mkdir decorate-test
$ cd decorate-test/
$ git init
Initialized empty Git repository in /xxxxxxx/decorate-test/.git/
$ touch README
$ git add README
$ git commit -m "Add README"
[master (root-commit) 50781c9] Add README
 1 file changed, 0 insertions(+), 0 deletions(-)
 create mode 100644 README
$ git log --oneline --decorate --graph
* 50781c9 (HEAD -> master) Add README
# Note the presence of the arrow in the output.

# Now, check out the commit directly to detach the HEAD:
$ git checkout 50781c9
Note: checking out '50781c9'.

You are in 'detached HEAD' state. You can look around, ...

HEAD is now at 50781c9... Add README
$ git log --oneline --decorate --graph
* 50781c9 (HEAD, master) Add README
# The arrow is gone!

# Check out master again to reattach the HEAD:
$ git checkout master
Switched to branch 'master'
$ git log --oneline --decorate --graph
* 50781c9 (HEAD -> master) Add README
# The arrow is back!