可以git log --decorate明确告诉我头部是否分离? [英] Can git log --decorate unambiguously tell me whether the HEAD is detached?

查看:153
本文介绍了可以git log --decorate明确告诉我头部是否分离?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我知道,在Git的说法中,detached HEAD对应于符号引用 HEAD 没有指向任何分支的状态。例如,我知道 git branch 会告诉我我是否处于detached-HEAD状态,例如

  *(从9a2ef02分离)
master

或不是,例如

  * master 

然而,我想知道是否有办法将 git log --decorate 的输出完全保存为无论我是否处于detached-HEAD状态,都是明确的。这里是一个例子,解释我的意思是毫不含糊。





示例



假设我在 master ,我的历史记录如下所示:

  4d860e9(HEAD,master)删除尾部空白符
9a2ef02正确的输入错误
f0badb5将README
添加到DO-DO部分



案例1 :unambiguous detached-HEAD state



如果我运行

  git checkout 9a2ef02 

然后输出 git log --decorate --oneline $ b $ $ $ $ $ $ $ $ $ $ $
$ b $ $

由于没有在 HEAD 在这个输出中,我确信我有一个分离的HEAD。



案例2:detached-HEAD状态与否?



但是,如果我运行

  git checkout 4d860e9 

然后 HEAD 并不指向 master ,但直接提交 4d860e9 ,其中 master 也指向;我有一个分离的头。但是,无法从的输出中得知git log --decorate --oneline



<$ p $删除尾部空白
9a2ef02在头文件中正确输入错误
f0badb5在README
中添加待执行部分> 4d860e9(HEAD,master) / pre>

,因为它与我在 master 时完全相同。



有没有办法通过 git log 选项来消除歧义?我在 git-log 手册页中找不到方法...

解决方案

使用Git 2.4+(Q2 2015), git log --decorate 将显示与HEAD相关的确切分支(或缺少它,对于分离的HEAD )。



请参阅提交51ff0f2 通过 Junio C Hamano( gitster


log :装饰 HEAD with分支名称



目前,日志装饰并不指示检出哪个分支
,以及 HEAD

当分支 foo 被签出时,将 HEAD,foo 部分装饰成 HEAD - > foo 。这表示参考装饰(由间隔帮助)以及它们的关系shsip。



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






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


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




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




HEAD引用输出中也显示其名称的分支的顶端时输出。
以上显示为:



pre $ g $ log $ dederate -1 master
commit bdb0f6788fa5e3cacc4315e9ff318a27b2676ff4(HEAD - > master)
...


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

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

解决方案

With Git 2.4+ (Q2 2015), git log --decorate will shows the exact branch associated with HEAD (or the lack thereof, for a detached HEAD).

See commit 51ff0f2 by Junio C Hamano (gitster):

log: decorate HEAD with branch name

Currently, log decorations do not indicate which branch is checked out and whether HEAD is detached.

When branch foo is checked out, change the "HEAD, foo" part of the decorations to "HEAD -> foo". This serves to indicate both ref decorations (helped by the spacing) as well as their relationshsip.

As a consequence, "HEAD" without any " -> " denotes a detached HEAD now.


This means the 2.4 release notes now include the following Backward compatibility warning(s):

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

这篇关于可以git log --decorate明确告诉我头部是否分离?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆