如何着色git的输出?

B S*_*ven 308 colors bash git

有没有办法为 git(或任何命令)的输出着色?

考虑:

baller@Laptop:~/rails/spunky-monkey$ git status
# On branch new-message-types
# Changes not staged for commit:
#   (use "git add <file>..." to update what will be committed)
#   (use "git checkout -- <file>..." to discard changes in working directory)
#
#       modified:   app/models/message_type.rb
#
no changes added to commit (use "git add" and/or "git commit -a")
baller@Laptop:~/rails/spunky-monkey$ git add app/models
Run Code Online (Sandbox Code Playgroud)

baller@Laptop:~/rails/spunky-monkey$ git status
# On branch new-message-types
# Changes to be committed:
#   (use "git reset HEAD <file>..." to unstage)
#
#       modified:   app/models/message_type.rb
#
Run Code Online (Sandbox Code Playgroud)

输出看起来相同,但信息完全不同:文件已从未暂存状态变为暂存状态以进行提交。

有没有办法给输出着色?例如,未暂存的文件是红色的,暂存的文件是绿色的?

或者甚至Changes not staged for commit:是红色和# Changes to be committed:绿色?

在 Ubuntu 工作。

编辑:谷歌搜索找到这个答案伟大的工程:git config --global --add color.ui true

但是,是否有更通用的解决方案来为命令输出添加颜色?

Mar*_*rco 320

您可以创建一个部分[color]~/.gitconfig与例如以下内容

[color]
  diff = auto
  status = auto
  branch = auto
  interactive = auto
  ui = true
  pager = true
Run Code Online (Sandbox Code Playgroud)

您还可以很好地控制要以何种方式着色的内容,例如

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

[color "branch"]
  remote = yellow
Run Code Online (Sandbox Code Playgroud)

我希望这能让你开始。当然,您需要一个支持颜色的终端。

  • 我认为将 git config --global color.ui auto (@Evgeny 的答案)放在你的顶部可能是值得的......我认为这可能是大多数人正在寻找的。我对两者都投了赞成票……我只是想说,为了这里的互联网,我想很多人只想要那种简单的衬里。如果他们得到它,那就更好了,再加上你的额外好处。 (21认同)
  • 您也可以将 [color] 部分添加到项目的 .git/config 文件中 (3认同)

Evg*_*eny 272

你可能想用

git config --global color.ui auto
Run Code Online (Sandbox Code Playgroud)

auto部分表示 git 只会尝试在支持它的终端上使用颜色,例如,如果将 git 命令的输出重定向到文件,则不会获得 ANSI 序列。将其设置true为与 相同auto,这也是自 Git 1.8.4 以来的默认设置。

color.ui是一个元配置,包括color.*git 命令可用的所有各种配置。

这在 中进行了深入解释git help config

  • @chharvey `color.ui = auto` + `git diff | less` - 没有颜色,`color.ui = always` + `git diff | less` - 彩色输出。`LESS=-R` 是隐含的。 (3认同)

sak*_*isk 24

git config --global color.ui auto
git config --global color.branch auto
git config --global color.status auto
Run Code Online (Sandbox Code Playgroud)


小智 22

接受的答案给出了最常见的解决方案。如果出于任何原因您不需要永久更改配置(该解决方案会这样做),您可以覆盖单个 git 命令的配置:

git -c color.ui=always <usual git command and options>
Run Code Online (Sandbox Code Playgroud)

例如:

git -c color.ui=always status
git -c color.ui=always diff
Run Code Online (Sandbox Code Playgroud)

经测试:在 git 2.4.6 上支持,在 git 1.7.1 上支持。

  • 对于其他人在像我一样通过管道输入 `less` 时试图获取颜色,您可以让 `less` 通过 `less -R` 将颜色转义字符传递到终端。 (3认同)

mli*_*bre 16

git config --global color.ui always
git config --global color.branch always
git config --global color.diff always
git config --global color.interactive always
git config --global color.status always
git config --global color.grep always
git config --global color.pager true
git config --global color.decorate always
git config --global color.showbranch always
Run Code Online (Sandbox Code Playgroud)