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

1

11

When I get the log of a GIT repo:

git log --oneline --decorate --graph

the output is like this:

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

In another repo, when I git log, I get:

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

What is the difference between (HEAD -> master) and (HEAD,master)

Seriema answered 20/5, 2016 at 10:50 Comment(0)
S
15

The arrow points to the current branch

An arrow to the right of HEAD, in the output of git log --oneline --decorate --graph, indicates which branch (if any) is the current one.

* 44025ed (HEAD -> master) second commit

means that the symbolic reference HEAD currently points to the master branch; in other words, you are not in detached-HEAD state, and the current branch is master.

enter image description here

In contrast,

* 44025ed (HEAD, master) second commit

means that the symbolic reference HEAD does not currently point to any branch, but to a commit (44025ed) directly; in other words, you are in detached-HEAD state. The master branch is only listed alongside HEAD because it happens to point to the same commit (44025ed).

enter image description here

Some history

For information, this distinction was introduced in Git (2.4) shortly after I asked the following question: Can git log --decorate unambiguously tell me whether the HEAD is detached?

A small experiment (to fix ideas)

$ 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!
Stanwood answered 20/5, 2016 at 10:56 Comment(4)
Great explanation! So we have the come when we are in detached-HEAD. If I am not wrong, I saw (HEAD, master) also in normal state while HEAD points to master (not detached-HEAD). Is it possible? PS: a big wow for the historical motivation! : )Seriema
If you see (HEAD, master), HEAD is not pointing to master; rather, HEAD and master are pointing at the same commit. I'll add pictures to my answer later today, for clarification.Stanwood
So, I think this means that, when my Git log says (HEAD -> Refactor, origin/Refactor, develop) beside a commit hash, this means that HEAD points to Refactor, Refactor points to the commit, and remote Refactor and develop are also pointing to this commit? And (in Git bash) green branches are local and red are remote?Deration
@Chucky Correct :)Stanwood

© 2022 - 2024 — McMap. All rights reserved.