有没有办法为 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)
我希望这能让你开始。当然,您需要一个支持颜色的终端。
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。
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 上不支持。
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)