git-log 中的颜色

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/5889878/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-10 10:51:42  来源:igfitidea点击:

Color in git-log

gitgit-log

提问by NorthIsUp

When you run git log --decorate --pretty=onelinethe output will have entries like (HEAD, refs/published/master, master)with coloration.

当您运行时git log --decorate --pretty=oneline,输出将包含(HEAD, refs/published/master, master)与着色类似的条目。

I also have the following in my gitconfig:

我的 gitconfig 中还有以下内容:

[color "branch"]
    current = yellow reverse
    local = yellow
    remote = green

How do you replicate those colors when doing a custom format like the following?

在执行如下自定义格式时,您如何复制这些颜色?

git log --decorate --stat --graph --pretty=format:"%d %Cgreen%h%Creset (%ar - %Cred%an%Creset), %s%n"

采纳答案by Elad Shahar

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

正如吉特1.8.3(2013年5月24日),你可以使用%C(auto)装饰%d中的格式字符串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.)

回答by VonC

The git log --decoratewill put by default:

git log --decorate会将默认:

  • the HEAD in cyan
  • the remote branches in red
  • the tag in green
  • 青色的 HEAD
  • 红色的远程分支
  • 绿色标签

and can be changed through color.decorateconfig.

并且可以通过color.decorate配置更改 。

But the git log --formatdon't offer a way to display specifically the HEADorremotes orbranch: all three are displayed through %d, with one color possible.

但是git log --format不提供一种专门显示HEAD遥控器分支的方法:所有三个都通过 显示%d,可能只有一种颜色。



Update May 2013, as mentioned belowby Elad Shahar(upvoted), git 1.8.3 offers one more option:

更新2013年5月,如下面提到埃拉德·沙哈尔(upvoted),Git的1.8.3提供多一个选择:

git log –formatnow sports a %C(auto)token that tells Git to use color when resolving %d(decoration), %h(short commit object name), etc. for terminal output.

git log –format现在运行一个%C(auto)令牌,它告诉 Git 在解析%d(装饰)、%h(短提交对象名称)等时使用颜色作为终端输出。

This Atlassian blog postcomments that this feature is part of several others focused on format (git rebase, git count-objects) and colors (git branch -vv)

这篇Atlassian 博客文章评论说,此功能是其他几个专注于格式 ( git rebase, git count-objects) 和颜色 ( git branch -vv)

This comes in addition of the previous auto,resetof 1.8.2, which automatically disables colors when the output is not used for a terminal1

这是auto,reset1.8.2之前版本补充,当输出不用于终端时自动禁用颜色1

%C(auto,blue)Hello%C(auto,reset)


Note: git 2.4+ (Q2 2015) will do a better job of resetting color around branch names.
See commit 5ee8758by Junio C Hamano (gitster):

注意:git 2.4+(2015 年第二季度)将更好地重置分支名称周围的颜色。
提交5ee8758通过JUNIOÇ滨野(gitster

log --decorate: do not leak "commit" color into the next item

In "git log --decorate", you would see the commit header like this:

log --decorate:不要将“提交”颜色泄漏到下一个项目中

在“ git log --decorate”中,您会看到这样的提交标头:

commit ... (HEAD, jc/decorate-leaky-separator-color)

where "commit ... (" is painted in color.diff.commit, "HEAD" in color.decorate.head, "," in color.diff.commit, the branch name in color.decorate.branchand then closing ")" in color.diff.commit.

If you wanted to paint the HEAD and local branch name in the same color as the body text (perhaps because cyan and green are too faint on a black-on-white terminal to be readable), you would not want to have to say

其中“ commit ... (”是画在color.diff.commit,“ HEAD”在color.decorate.head,“ ,”在color.diff.commit,分支名称在 color.decorate.branch,然后关闭“ )”在color.diff.commit

如果您想将 HEAD 和本地分支名称绘制为与正文文本相同的颜色(可能是因为在黑白终端上青色和绿色太暗而无法阅读),您不必说

[color "decorate"]
    head = black
    branch = black

because that you would not be able to reuse same configuration on a white-on-black terminal. You would naively expect

因为您将无法在黑底白字的终端上重复使用相同的配置。你会天真地期待

[color "decorate"]
    head = normal
branch = normal

to work, but unfortunately it does not.
It paints the string "HEAD" and the branch name in the same color as the opening parenthesis or comma between the decoration elements.
This is because the code forgets to reset the color after printing the "prefix" in its own color.

工作,但不幸的是它没有。
它用HEAD与装饰元素之间的左括号或逗号相同的颜色绘制字符串“ ”和分支名称。
这是因为代码在用自己的颜色打印“前缀”后忘记重置颜色。



Note that git 2.5 (Q2 2015) fixes a bug:

请注意,git 2.5(2015 年第二季度)修复了一个错误:

See commit 429ad20by Junio C Hamano (gitster), 13 May 2015.
(Merged by Junio C Hamano -- gitster--in commit fd70780, 22 May 2015)

看看提交429ad20通过JUNIOÇ滨野(gitster,2015年5月13日
(通过合并JUNIOÇ滨野- gitster-提交fd707802015,5月22日)

log: do not shorten decoration names too early

The "log --decorate" enhancement in Git 2.4 that shows the commit at the tip of the current branch e.g. "HEAD -> master", did not work with --decorate=full.

log: 不要过早缩短装饰名称

log --decorateGit 2.4 中的“ ” 增强功能在当前分支的尖端显示提交,例如“ HEAD -> master”,不适用于 --decorate=full。



Git 2.9.x+ (Q3 2016) will fix another bug and honor color=autofor %C(auto)

Git的2.9.x +(Q3 2016)将解决另一个bug和荣誉color=auto%C(auto)



Git 2.10.2 (Oct. 2016) fixes other bugs with commit 82b83da(29 Sep 2016), and commit c99ad27(17 Sep 2016) by René Scharfe (``).
(Merged by Junio C Hamano -- gitster--in commit 76796d4, 28 Oct 2016)

Git 2.10.2(2016 年 10 月)修复了其他错误,提交 82b83da(2016 年 9 月 29 日),并由René Scharfe (``)提交 c99ad27(2016 年 9 月 17 日
(由Junio C gitsterHamano合并-- --commit 76796d4,2016 年 10 月 28 日)

pretty: avoid adding reset for %C(auto)if output is empty

We emit an escape sequence for resetting color and attribute for %C(auto)to make sure automatic coloring is displayed as intended.
Stop doing that if the output strbuf is empty, i.e. when %C(auto)appears at the start of the format string, because then there is no need for a reset and we save a few bytes in the output.

pretty: let %C(auto)reset all attributes

Reset colors and attributesupon %C(auto)to enable full automatic control over them; otherwise attributes like bold or reverse could still be in effect from previous %Cplaceholders.

pretty:%C(auto)如果输出为空,则避免添加重置

我们发出一个用于重置颜色和属性的转义序列,%C(auto)以确保按预期显示自动着色。
如果输出 strbuf 为空,即当%C(auto)出现在格式字符串的开头时,请停止这样做,因为这样就不需要重置,我们在输出中保存了几个字节。

pretty: 让%C(auto)重置所有属性

复位颜色和属性%C(auto),以使他们在全自动控制; 否则像 bold 或 reverse 这样的属性仍然可以从以前的%C占位符中生效

回答by Josh Lee

Parenthesize them:

将它们括起来:

%C(...): color specification, as described in color.branch.* config option

%C(...): color specification, as described in color.branch.* config option

So %C(yellow reverse)would work.

所以%C(yellow reverse)会工作。

回答by Henrik Gustafsson

The config option log.decoratecan enable/disable default decorations in logs.

配置选项log.decorate可以启用/禁用日志中的默认装饰。

git config --global log.decorate full

Once that is done you can use color.decorate.*to play with the colors

完成后,您可以使用color.decorate.*颜色来玩

回答by NullPointerWizard

Some may want to use this : %C(colorname)This doesn't need to change the color config.

有些人可能想使用这个:%C(colorname)这不需要更改颜色配置。

Example : Coloring the author name in yellow

示例:将作者姓名涂成黄色

--pretty=format:"%C(yellow)%an%Creset"

Regular ANSI colors should work https://en.wikipedia.org/wiki/ANSI_escape_code

常规的 ANSI 颜色应该可以使用https://en.wikipedia.org/wiki/ANSI_escape_code

  • black
  • red
  • green
  • yellow
  • blue
  • magenta
  • cyan
  • white
  • 黑色的
  • 红色的
  • 绿色
  • 黄色
  • 蓝色
  • 品红
  • 青色
  • 白色的