如何为git-status输出着色


92

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

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

相反,我看到绿色的暂存文件和蓝色的未暂存文件: git-status的屏幕截图

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

[color]
status = auto

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

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

下面的答案是否解决了您的问题?
— VonC

Answers:


130

从git config doc:

color.status.<slot>

使用自定义颜色进行状态着色。<slot>是其中之一:

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

这些变量的值可以在中指定color.branch.<slot>。

所以这将工作:

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

然而:

new files = green
deleted files = red

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

  • 如果将它们添加到索引中,它们将为选择颜色color.status.added。
  • 如果未将它们添加到索引中,则会选择颜色或 color.status.modified。

当然,作为评论的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月)中,输出着色方案学习了两个新属性,斜体和罢工,以及现有的粗体,反向等

请参阅Jeff King()的提交9dc3515,提交54590a0,提交5621068,提交df8e472,提交ae989a6,提交adb3356,提交0111681(2016年6月23日)。(由Junio C Hamano合并--在commit 3c5de5c中,2016年7月11日)peff
gitster

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

使用“ no-bold”而不是“ nobold”更易于阅读,而且键入起来更自然(无论如何,对我而言,即使我是首先引入“ nobold”的人)。两者都容易做到。


30
如果以前未启用,请记住启用着色输出:git config --global color.ui true
— elboletaire 2013年

1
@elboletaire好点。我将其包含在答案中以提高可见性。
— VonC

1
谢谢,color.ui是答案。
— Robeezy
By using our site, you acknowledge that you have read and understand our Cookie Policy and Privacy Policy.
Licensed under cc by-sa 3.0 with attribution required.