'Can git log --decorate unambiguously tell me whether the HEAD is detached?
I know that, in Git parlance, "detached HEAD" corresponds to a state in which the symbolic reference HEAD is not pointing to any branch. I also know that git branch, for instance, will tell me whether I'm in detached-HEAD state, e.g.
* (detached from 9a2ef02)
master
or not, e.g.
* master
However, I'd like to know if there is a way to make the output of git log --decorate completely unambiguous as to whether I'm in detached-HEAD state or not.
Example
Say I'm on master and my history looks as follows:
4d860e9 (HEAD, master) Remove trailing whitespace
9a2ef02 Correct typo in header
f0badb5 Add to-do section to README
Case 1: unambiguous detached-HEAD state
If I run
git checkout 9a2ef02
then the output of git log --decorate --oneline is
9a2ef02 (HEAD) Correct typo in header
f0badb5 Add to-do section to README
Because no branch reference is listed next to HEAD in this output, I know for sure that I've got a detached HEAD.
Case 2: detached-HEAD state or not?
However, if I run
git checkout 4d860e9
then HEAD does not point to master, but directly to commit 4d860e9, which master also points to; I've got a detached HEAD. However, there is no way to tell from the output of git log --decorate --oneline,
4d860e9 (HEAD, master) Remove trailing whitespace
9a2ef02 Correct typo in header
f0badb5 Add to-do section to README
because it's exactly the same as when I'm on master.
Is there a way, via some git log options, to remove that ambiguity? I haven't found a way in the git-log man page...
Solution 1:[1]
[Edit: since Git 2.4, well, see VonC's answer. The text below is for versions of Git before 2.4.]
Unfortunately, no. I keep wishing git log's --decorate used my HEAD= syntax. If it did, you would get:
4d860e9 (HEAD, master) Remove trailing whitespace
9a2ef02 Correct typo in header
f0badb5 Add to-do section to README
when you you're in detached-HEAD state, but you would get this instead:
4d860e9 (HEAD=master) Remove trailing whitespace
9a2ef02 Correct typo in header
f0badb5 Add to-do section to README
otherwise.
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | jonrsharpe |
