【问题标题】:How to colorize git-status output?如何为 git-status 输出着色?
【发布时间】:2012-10-09 08:24:03
【问题描述】:

我想对 git-status 输出着色,以便:

untracked files = magenta
new files = green
modified files = blue
deleted files = red

我看到的是绿色的暂存文件和蓝色的未暂存文件:

我的 .gitconfig 是根据一些搜索设置的:

[color]
status = auto

[color "status"]
added = green
changed = blue
untracked = magenta
deleted = red

【问题讨论】:

  • 注:git 2.9.1 支持属性斜体和罢工。请参阅下面的编辑。

标签: git git-config git-status


【解决方案1】:

来自git config doc

color.status.<slot>

使用自定义颜色进行状态着色。 &lt;slot&gt; 是其中之一:

  • header(状态消息的标题文本),
  • addedupdated(已添加但未提交的文件),
  • changed(已更改但未添加到索引中的文件),
  • untracked(git 不跟踪的文件),
  • branch(当前分支),
  • nobranch(无分支警告显示颜色,默认为红色),
  • localBranchremoteBranch(分别为本地和远程分支名称,当分支和跟踪信息以状态短格式显示时),
  • unmerged(具有未合并更改的文件)。

这些变量的值可以指定为color.branch.&lt;slot&gt;

所以这会起作用:

git config color.status.changed blue
git config color.status.untracked magenta

但是:

new files = green
deleted files = red

不可能:你需要选择一种颜色:

  • 如果他们被添加到索引中,他们将使用color.status.added的颜色。
  • 如果它们未添加到索引中,它们将使用color.status.changed 的颜色。

注意:

颜色也可以用 0 到 255 之间的数字表示;这些使用 ANSI 256 色模式(但并非所有终端都支持此模式)。
这些数字见“xterm 256 colors”,如the comments Joshua Goldberg 所述。


当然,就像commented by elboletaire

如果之前没有启用着色输出,记得启用它:

git config --global color.ui true

Shaun Luttin 补充:

该命令还可以使用引号中的多个参数。这包括此列表中的两种颜色(前景背景):

正常、黑色、红色、绿色、黄色、蓝色、品红色、青色和白色;

它还包括此列表中的一个属性(样式):

粗体、暗淡、ul、闪烁和反转。

所以这会起作用:

git config color.status.changed "blue normal bold"
git config color.status.header "white normal dim"

注意:使用 git 2.9.1(2016 年 7 月),输出着色方案学习了 两个新属性,italicstrike,在除了现有的粗体、反转等。

参见commit 9dc3515commit 54590a0commit 5621068commit df8e472commit ae989a6commit adb3356commit 0111681(2016 年 6 月 23 日)Jeff King (peff)
(由Junio C Hamano -- gitster --commit 3c5de5c,2016 年 7 月 11 日)

它还允许使用“no-”来否定属性

使用“no-bold”而不是“nobold”更容易阅读和输入更自然(对我来说,无论如何,即使我是首先介绍“nobold”的人)。两者都很容易。

【讨论】:

  • 如果之前没有开启着色输出,记得开启:git config --global color.ui true
  • @elboletaire 好点。我已将其包含在答案中以提高知名度。
  • 谢谢,color.ui 就是答案。
  • 您的意思是写 color.status.changed 而不是 color.status.modified?还是两者都有效?
  • @alper Reading unix.stackexchange.com/a/19320/7490,我建议:git -c color.status=always status -sb --ignore-submodules=dirty | less -rFX
猜你喜欢
  • 1970-01-01
  • 2013-08-27
  • 1970-01-01
  • 1970-01-01
  • 2010-11-29
  • 2011-01-20
  • 1970-01-01
  • 1970-01-01
  • 2015-08-26
相关资源
最近更新 更多