如何模拟git log --decorate的不同分支类型颜色?

67

在制作我最喜欢的 git 日志视图时,我创建了这个别名:

graph = log --pretty=format:'%Cgreen%ad%Creset %C(yellow)%h%Creset%C(yellow)%d%Creset %s %C(cyan)[%an]%Creset %Cgreen(%ar)%Creset' --date=short --graph

这将创建如下输出:

Git log result with own coloring

我在这里缺少的是分支类型的不同着色,就像log --oneline --decorate --graph中的一样。

Git log result with --decorate(=short)

--decorate(默认使用=short)会给不同的已识别分支着上不同的颜色。分支类型 (HEAD, origin/master, origin/HEAD, master) 着上青色、红色、红色和绿色。然而,我的分支未被着色;只用整个分支部分的黄色进行了染色。

是否有一种方法可以通过自己的别名为不同的分支类型赋予不同的颜色?

2个回答

99

https://dev59.com/Hm025IYBdhLWcg3wpHl-#16844346

As of git 1.8.3 (May 24, 2013), you can use %C(auto) to decorate %d in the format string of git log.

From the release notes:

 * "git log --format" specifier learned %C(auto) token that tells Git
   to use color when interpolating %d (decoration), %h (short commit
   object name), etc. for terminal output.)

5
太棒了!我寻找了几个小时,谢谢!针对 Google,需要添加一些关键词:%d、引用名称、--oneline。 - astgtciv
2
谢谢您。非常好的隐藏宝石! - Munter
很不幸,我被困在版本1.7.12.4 :( 所以 %d 和 %C(auto) 无法使用。虽然 %ad %an %h ... 等颜色可以单独定义,但我在分支名称的颜色上遇到了麻烦。有没有想法在 oneline 的字符串格式中找到它? - Radu
1
这正是我正在寻找的。我差点用了困难的方法,但你刚刚为我节省了很多努力! :-) - Douglas Muth

1
我不相信这是可能的。浏览git-log(1)手册,--pretty 的唯一相关格式似乎是 %d,它会给出所有引用名称。由于没有区分引用类型的选项,因此您无法为每种类型分配不同的颜色。
另请参见此问题

1
这个答案已经不再正确。自 git 1.8.3(2013年5月24日)起,您可以在 git log 的格式字符串中使用 %C(auto) 来装饰 %d。 - reducing activity

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