git log --decorate 能够明确地告诉我 HEAD 是否处于分离状态吗?

7

我知道,在Git的术语中,“分离头指针”对应的状态是符号引用HEAD没有指向任何分支。我还知道,例如git branch会告诉我是否处于分离头指针状态。

* (detached from 9a2ef02)
  master

或者不,例如。
* master

然而,我想知道是否有一种方法可以使git log --decorate的输出完全清晰无误地表明我的状态是分离HEAD状态还是不是。

示例

假设我在master上,并且我的历史记录如下:

4d860e9 (HEAD, master) Remove trailing whitespace
9a2ef02 Correct typo in header
f0badb5 Add to-do section to README

案例1:明确的分离HEAD状态

如果我运行

git checkout 9a2ef02

那么执行 git log --decorate --oneline 命令的输出结果为:

9a2ef02 (HEAD) Correct typo in header
f0badb5 Add to-do section to README

因为在此输出中没有列出任何分支引用与HEAD相邻,所以我确定我已经处于分离的HEAD状态。

情况2:分离的HEAD状态还是不是?

然而,如果我运行

git checkout 4d860e9

那么HEAD就不指向master,而是直接指向提交4d860e9,而master也指向这个提交;我现在处于分离头状态。然而,从git log --decorate --oneline的输出中无法判断。

4d860e9 (HEAD, master) Remove trailing whitespace
9a2ef02 Correct typo in header
f0badb5 Add to-do section to README

因为它与我在master分支上时完全相同。

是否有一种方法,通过一些git log选项来消除这种不确定性?我在git-log手册中没有找到方法...


答案必须是针对 git log 吗?有其他方法可以判断你是否处于分离 HEAD 状态,例如使用 git symbolic-ref HEAD - Greg Hewgill
@GregHewgill 是的,我知道 git symbolic-ref,但我特别询问的是 git log/git show - jub0bs
1
+1 for the illustrative picture以下是有关编程的内容,请将其翻译成中文。 - Lajos Arpad
这将在Git 2.4(2015年第二季度)中实现。请参见下面的我的回答 - VonC
2个回答

4

从Git 2.4+(2015年第二季度)开始,git log --decorate会显示与HEAD关联的确切分支(或者对于游离的HEAD,缺失的分支)。

请参见提交51ff0f2,由Junio C Hamano (gitster)撰写:

log:用分支名称装饰HEAD

当前,日志装饰不会显示检出了哪个分支以及HEAD是否分离。

当检出分支foo时,将装饰中的"HEAD, foo"部分更改为"HEAD -> foo"。这有助于指示引用装饰(通过间距帮助)以及它们之间的关系。

因此,现在没有任何"->"的"HEAD"表示分离的HEAD


这意味着2.4版发行说明现在包含以下向后兼容性警告:

Output from "git log --decorate" (and "%d" format specifier used in the userformat "--format=<string>" parameter "git log" family of command takes) used to list "HEAD" just like other tips of branch names, separated with a comma in between. E.g.

$ git log --decorate -1 master
commit bdb0f6788fa5e3cacc4315e9ff318a27b2676ff4 (HEAD, master)
...

This release updates the output slightly when HEAD refers to the tip of a branch whose name is also shown in the output.
The above is shown as:

$ git log --decorate -1 master
commit bdb0f6788fa5e3cacc4315e9ff318a27b2676ff4 (HEAD -> master)
...

1
是的,这是一个受欢迎的改进。我相信我在其中扮演了(小)角色 :)。 - jub0bs
1
@Jubobs 做得好!我只是不明白为什么这个线程(http://comments.gmane.org/gmane.comp.version-control.git/263922)以“自 Git 2.3.0 起”开头?在 Git 2.3.0 之前就不行吗? - VonC
我的措辞不太恰当;Junio Hamano 也被措辞搞混了。我的意思是:“我们现在已经到了 Git 2.3.0,但这个功能还没有被实现。” - jub0bs

2
很抱歉,不行。我一直希望git log--decorate使用我的HEAD=语法,但实际上并没有。如果它这样做了,你会得到:
4d860e9 (HEAD, master) Remove trailing whitespace
9a2ef02 Correct typo in header
f0badb5 Add to-do section to README

当你处于分离的HEAD状态时,但你会得到这个:
4d860e9 (HEAD=master) Remove trailing whitespace
9a2ef02 Correct typo in header
f0badb5 Add to-do section to README

否则。

为了解情况,我在邮件列表上向Git开发团队询问他们是否认为这是一个好的功能:http://marc.info/?l=git&m=142412655130612&w=2 - jub0bs
您的答案当时是正确的,但我想更多地展示新情况。对于取消采纳您的答案请谅解。 - jub0bs
1
@Jubobs:这就是正在不断变化的软件,没办法啊。 :-) - torek

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接