当我获得 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
分支;换句话说,您不处于 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!
关于git - git log --decorate : (HEAD -> master) vs (HEAD, master 的输出差异),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37344763/