我最近看到 Windows 中的 git
控制台是彩色的,例如绿色表示添加,红色表示删除等。我如何为我的 git
控制台着色?
为了安装它,我使用了命令:$ sudo apt-get install git-core
作为 @VonC 的 noted,color.ui
自 Git 1.8.4 起默认为 auto
来自 Unix & Linux Stackexchange 问题 How to colorize output of git? 和 answer by @Evgeny:
git config --global color.ui auto
color.ui 是一个元配置,包括 git 命令可用的所有各种 color.* 配置。这在 git help config 中有深入的解释。
因此,基本上它比单独设置不同的 color.*
设置更容易且更具前瞻性。
git config
documentation 的深入解释:
color.ui:此变量确定控制每个命令系列颜色使用的变量的默认值,例如 color.diff 和 color.grep。随着更多命令学习配置以设置 --color 选项的默认值,其范围将扩大。如果您希望所有不打算供机器使用的输出使用颜色,请将其设置为始终;如果您希望此类输出在写入终端时使用颜色,则设置为 true 或 auto;如果您更喜欢 git 命令不使用颜色,则设置为 false 或 never除非使用其他配置或 --color 选项显式启用。
例如参见 https://web.archive.org/web/20080506194329/http://www.arthurkoziel.com/2008/05/02/git-configuration/
有趣的部分是
彩色输出: git config --global color.branch auto git config --global color.diff auto git config --global color.interactive auto git config --global color.status auto
color.ui auto
对我不起作用,但确实如此。谢谢你。
将下一个代码添加到您的 .gitconfig 文件中:
[color]
ui = auto
[color "branch"]
current = yellow reverse
local = yellow
remote = green
[color "diff"]
meta = yellow bold
frag = magenta bold
old = red bold
new = green bold
[color "status"]
added = yellow
changed = green
untracked = cyan
如果您要求,Git 会自动为其大部分输出着色。您可以非常具体地了解您想要什么颜色以及如何上色;但要打开所有默认终端着色,请将 color.ui 设置为 true:
git config --global color.ui true
在 Ubuntu 或任何其他平台中(是的,Windows 也是如此!);从 git1.8.4(即 released 2013-08-23)开始,您无需执行任何操作:
许多教程教用户将“color.ui”设置为“auto”作为您设置“user.name/email”后的第一件事,以便向 Git 介绍自己。现在变量默认为“自动”。
所以你会默认看到颜色。
git config --global color.ui auto
。我的 Mac 也是如此,唯一默认为 auto
的是我的 Windows PC 上的 Git Bash。
在您的 ~/.gitconfig
文件中,只需添加以下内容:
[color]
ui = auto
它会处理你所有的 git 命令。
另一种方法是编辑 .gitconfig
(如果不存在则创建一个),例如:
vim ~/.gitconfig
然后添加:
[color]
diff = auto
status = auto
branch = auto
[color] ui = auto
就足够了。
GIT 默认使用彩色输出,但在某些系统(如 CentOS)上未启用。您可以像这样启用它
git config --global color.ui true
git config --global color.ui false
git config --global color.ui auto
您可以从这里选择您需要的命令。
这里 --global 是可选的,可以对系统中的每个存储库应用操作。如果您只想为当前存储库应用着色,那么您可以执行以下操作 -
git config color.ui true
改进 Windows 10 上的 Git 'branch-type' 颜色:
显示命令 git branch -avv
所有示例...
1. Git 默认配色方案:
git config --system --remove-section color.branch
https://i.stack.imgur.com/BP9ue.png
<强> 2。此页面上另一个 answer 中给出的分支颜色:
git config --system color.branch.local "yellow" git config --system color.branch.current "yellow reverse" git config --system color.branch.remote "green"
https://i.stack.imgur.com/h8XRO.png
3.可能改进的分支配色方案:
git config --system color.branch.local "yellow" git config --system color.branch.current "brightwhite yellow" git config --system color.branch.remote "normal green" git config --system color.branch.upstream "brightwhite cyan"
https://i.stack.imgur.com/G2BK2.png
4. 自己制作:
指定前景色和(可选)背景色。在 Windows 10 中有效的一组颜色是根据以下正则表达式给出的(是的,您可以使用 brightblack
,它实际上非常有用):
(normal|(bright)?(black|red|green|yellow|blue|magenta|cyan|white))
据我所知,dim
选项(请参阅底部的文档链接)在 Windows 10 控制台中没有任何作用,bold
选项与 bright*
颜色具有相同的效果。现在回想一下示例中显示的配置语法:
git config <config-type> color.branch.<slot> "<fg> <bg>"
参数 <config-type>
通常是 --system
或 --global
。如果省略,则指定的颜色将仅应用于当前存储库。使用刚刚详述的颜色名称,您可以为特定分支类型设置前景 <fg>
和背景 <bg>
颜色,其中 <slot>
是以下之一:
当前(当前分支)
本地(本地分支)
远程(refs/remotes/ 中的远程跟踪分支)
上游(上游跟踪分支)
清楚的
与往常一样,您可以通过以下命令显示整个 git 配置,其中将包括您在此处描述的设置的任何选项:
git config -l --show-origin
请注意,还有一组与日志输出颜色有关的配置选项(此处未讨论):
git config --system color.decorate.(branch|remoteBranch|tag|stash|HEAD|grafted) <color>
参考:https://git-scm.com/docs/git-config
好吧,如果你对默认设置不满意,可以使用 ANSI escape code 来帮助你设置颜色,如果你想修改一些文本,你可以写 bash 来帮助你。见下图:
样例
# .gitconfig
[alias]
st-color = "!f() { \
echo -n -e '\\033[38;2;255;0;01m\\033[4m' ;\
git status -s | grep ' D' | \
sed -e 's/^ ./DELETE:/' ; \
echo -n -e '\\033[m' ;\
\
echo -n -e '\\033[48;2;128;128;128m' ;\
echo -n -e '\\033[38;2;0;255;01m' ;\
git status -s | grep ' [AM]' | \
sed -e 's/^ ./NEW OR MODIFY:/' ; \
echo -n -e '\\033[m' ;\
\
echo -n -e '\\033[38;2;255;0;255m' ;\
echo Rename ;\
git status -s | grep 'R ' | \
sed -e 's/^..//' ; \
echo -n -e '\\033[m' ;\
}; f"
演示
https://i.stack.imgur.com/GCmS2.png
解释
您可以在 .gitconfig 上编写长脚本,使用如下语法: [alias] your-cmd = !f() { \ \ }; f" echo -n -e (查看更多 echo) -n = 不输出尾随换行符。-e 启用对以下反斜杠转义的 \\033[38;2;255;0;0m\\033[4m 的解释(查看更多 SGR 参数) \\033[38;2;255;0;0m : 38 表示前景色。255;0;0 = Red | r;g;b \\033[4m : underline grep : grep 命令用于搜索文本 sed -e 's/be_replace_string/new_string/' 将字符串替换为新字符串。
使用 Git 2.18,您可以更好地控制在控制台中指定颜色的方式。
“git config
”命令使用单独的选项,例如“--int
”、“--bool
"等指定调用者希望将值解释为什么类型。
引入了一个新的“--type=<typename>
”选项,这将使定义新类型更加简洁。
请参阅 Taylor Blau (ttaylorr
) 的 commit fb0dc3b(2018 年 4 月 18 日)和 commit 0a8950b(2018 年 4 月 9 日)。
(由 Junio C Hamano -- gitster
-- 在 commit e3e042b 中合并,2018 年 5 月 8 日)
builtin/config.c:支持 --type=
因此,在您拥有 --bool
和 --int
之前,现在 (documentation):
--type <type>
'git config' 将确保任何输入或输出在给定的类型约束下都是有效的,并将规范化
--bool::
--int::
--bool-or-int::
--path::
--expiry-date::
Historical options for selecting a type specifier. Prefer instead `--type`,
(see: above).
请注意,Git 2.22(2019 年第二季度)解释说“git config --type=color ...
”是为了替换“git config --get-color
”,但有一点没有记录在案,现在已修复。
请参阅 Jeff King (peff
) 的commit cd8e759(2019 年 3 月 5 日)。
(由 Junio C Hamano -- gitster
-- 在 commit f6c75e3 中合并,2019 年 3 月 20 日)
config: document --type=color output is a complete line 即使“git config”的较新的“--type=color”选项是为了向上兼容传统的“--get-color”选项,不像后者,它的输出不是最后缺少 LF 的不完整行。这使其与“git config --type=bool”等其他类型的输出一致。记录它,因为它有时会让毫无戒心的用户感到惊讶。
现在是这样写的:
--type=color [--default=
您可以看到 git config --type=bool
与 Git 2.26(2020 年第一季度)一起用于替换示例模板中的“git config --bool
”调用。
请参阅 Lucius Hu (lebensterben
) 的commit 81e3db4(2020 年 1 月 19 日)。
(由 Junio C Hamano -- gitster
-- 在 commit 7050624 中合并,2020 年 1 月 30 日)
模板:修复不推荐使用的类型选项 --bool 签名者:Lucius Hu git-config 的 --bool 选项被标记为历史,建议用户使用 --type=bool 代替。此提交替换模板中所有出现的 --bool 。另请注意,没有找到其他不推荐使用的类型选项,包括 --int、--bool-or-int、--path 或 --expiry-date。
参考这里:https://nathanhoad.net/how-to-colours-in-git/
脚步:
打开 ~/.gitconfig 进行编辑 vi ~/.gitconfig 粘贴以下代码: [color] ui = auto [color "branch"] current = yellow reverse local = yellow remote = green [color "diff"] meta = yellow bold frag =洋红色粗体 旧 = 红色粗体 新 = 绿色粗体 [颜色“状态”] 添加 = 黄色已更改 = 绿色未跟踪 = 青色 保存文件。
只需更改本地存储库中的任何文件并执行
git status
假设您希望当前分支为黄色,所有其他分支为青色粗体。我正在考虑您希望这些更改在本地完成,即在您当前的存储库中完成,而不是针对您系统中存在的所有存储库。使用“cd .git”进入 .git 文件,然后打开“config”文件。在配置文件中键入以下内容,而不更改配置文件中的任何其他内容。
[color]
ui=true
[color "branch"]
local=cyan bold
current=yellow bold
然后保存配置文件。打开 git 控制台并执行 git 分支。你会看到区别
不定期副业成功案例分享
auto
就足够了。ui = auto
条目添加到用户的~/.gitconfig
文件的[color]
部分。