7

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. Here is an example explaining what I mean by "unambiguous".

enter image description here

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...

4

2 回答 2

4

使用 Git 2.4+(2015 年第二季度),git log --decorate将显示与 HEAD 关联的确切分支(或缺少分支,对于分离的 HEAD)。

请参阅Junio C Hamano ( )提交 51ff0f2gitster

log:HEAD用分支名称装饰

目前,日志装饰不指示签出哪个分支以及是否HEAD分离。

foo签出分支时,HEAD, foo将装饰的“”部分更改为“ HEAD -> foo”。这用于指示参考装饰(由间距帮助)以及它们的关系。

因此,“ HEAD”没有任何“ ->”表示一个分离的HEAD现在


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

来自“ git log --decorate”(和“用户格式” “参数” “命令系列中%d使用的“”格式说明符)的输出用于列出“ ”,就像分支名称的其他提示一样,中间用逗号分隔。例如--format=<string>git logHEAD

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

当 HEAD 引用名称也显示在输出中的分支的尖端时,此版本会稍微更新输出。
上图显示为:

$ git log --decorate -1 master
commit bdb0f6788fa5e3cacc4315e9ff318a27b2676ff4 (HEAD -> master)
...
于 2015-03-23T09:50:15.900 回答
2

[编辑:从 Git 2.4 开始,好吧,请参阅VonC 的回答。以下文字适用于2.4之前的 Git 版本。]

抱歉不行。我一直希望git log's--decorate使用我的HEAD=语法。如果是这样,你会得到:

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

当你把头夹在腋下时:


(来源:shutterstock.com

但你会得到这个:

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

当你不在万圣节模式时。

于 2014-08-19T21:08:16.580 回答