gpt4 book ai didi

git - git log --decorate 可以明确地告诉我 HEAD 是否分离吗?

转载 作者:太空狗 更新时间:2023-10-29 13:11:02 24 4
gpt4 key购买 nike

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

* (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:明确的 detached-HEAD 状态

如果我跑

git checkout 9a2ef02

那么 git log --decorate --oneline 的输出是

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

因为在此输出中 HEAD 旁边没有列出分支引用,所以我确信我有一个分离的 HEAD。

情况 2:是否为 detached-HEAD 状态?

但是,如果我运行

git checkout 4d860e9

然后HEAD不指向master,而是直接提交4d860e9master 也指向;我有一个独立的 HEAD。但是,无法从 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 2.4+(2015 年第 2 季度),git log --decorate将显示与 HEAD 相关联的确切分支(或者对于分离的 HEAD,则缺少相关分支)。

参见 commit 51ff0f2通过 Junio C Hamano ( gitster ) :

log: decorate HEAD with branch name

Currently, log decorations do not indicate which branch is checked outand 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.


这意味着 the 2.4 release notes现在包含以下向后兼容性警告:

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 - git log --decorate 可以明确地告诉我 HEAD 是否分离吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25392580/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com