ChatGPT解决这个技术问题 Extra ChatGPT

如何为 Git 控制台着色?

我最近看到 Windows 中的 git 控制台是彩色的,例如绿色表示添加,红色表示删除等。我如何为我的 git 控制台着色?

为了安装它,我使用了命令:$ sudo apt-get install git-core

从 git1.8.4 开始,您应该会默认看到颜色。请参阅my answer below
@VonC git 1.9.1 在 Ubuntu 14.04 上,没有发生。必须自己从 JoelPurra 的回答中设置配置。
@Izkata 很奇怪,我稍后会测试它,但是 git 2.1+ 呢? (正如我在 stackoverflow.com/questions/10998792/… 中评论的)
对于颜色值、属性等,这是一个非常有用的链接:mirrors.edge.kernel.org/pub/software/scm/git/docs/…

M
MauroPorras

作为 @VonCnotedcolor.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 选项显式启用。


这也适用于 OSX,而不仅仅是问题所问的 linux
@Skeptor:不,auto 就足够了。
@Phani:是的,它是持久的。
它是持久的,因为它将 ui = auto 条目添加到用户的 ~/.gitconfig 文件的 [color] 部分。
加 1 用于给予早期答案的信用/参考。
s
stickyuser

例如参见 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


我正在使用旧版本的 git 并且设置 color.ui auto 对我不起作用,但确实如此。谢谢你。
W
Waldir Leoncio

将下一个代码添加到您的 .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

T
Tanmay

如果您要求,Git 会自动为其大部分输出着色。您可以非常具体地了解您想要什么颜色以及如何上色;但要打开所有默认终端着色,请将 color.ui 设置为 true:

git config --global color.ui true

J
Joel Purra

在 Ubuntu 或任何其他平台中(是的,Windows 也是如此!);从 git1.8.4(即 released 2013-08-23)开始,您无需执行任何操作

许多教程教用户将“color.ui”设置为“auto”作为您设置“user.name/email”后的第一件事,以便向 Git 介绍自己。现在变量默认为“自动”。

所以你会默认看到颜色。


(嗯,甚至是 Windows,取决于终端:stackoverflow.com/a/12133244/6309
2014 年使用 Ubuntu,安装了 git,但仍然必须运行 git config --global color.ui auto。我的 Mac 也是如此,唯一默认为 auto 的是我的 Windows PC 上的 Git Bash。
@sargas 当然,您需要安装 from a PPAlaunchpad.net/~git-core/+archive/ppa(适用于 Ubuntu)或 git-scm.com/download/mac(适用于 Mac)
我明白了,所以这取决于来源。感谢您抽出宝贵时间对此发表评论。
S
Skiptomylu

在您的 ~/.gitconfig 文件中,只需添加以下内容:

[color]
  ui = auto

它会处理你所有的 git 命令。


k
kenorb

另一种方法是编辑 .gitconfig(如果不存在则创建一个),例如:

vim ~/.gitconfig

然后添加:

[color]
  diff = auto
  status = auto
  branch = auto

正如@chuntao-lu 提到的 [color] ui = auto 就足够了。
D
Deepak Dixit

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 

G
Glenn Slayden

改进 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


C
Carson

好吧,如果你对默认设置不满意,可以使用 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/' 将字符串替换为新字符串。


link 是另一个示例。
V
VonC

使用 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= 作为 -- 的首选别名 git config 长期以来一直允许调用者提供“类型说明符”,它指示 git config (1) 确保传入值可以解释为该类型,并且 (2) 传出值在该类型下被规范化。在另一个系列中,我们建议使用--type=color 和--default 来扩展此功能以替换--get-color。然而,我们传统上使用 --color 来表示“为这个输出着色”,而不是“这个值应该被视为一种颜色”。目前, git config 不支持这种着色,但我们应该注意避免过早地蹲在这个选项上,以便 git config 将来可以支持 --color (传统意义上的),如果需要的话。在这个补丁中,除了 --int、--bool 等之外,我们还支持 --type=。这使得上述即将发布的补丁能够支持查询颜色通过 --type=color --default=... 使用默认值,而不浪费 --color。当给出多个遗留样式 -- 标志时,我们保留了抱怨的历史行为,并将其扩展到冲突的新样式 --type= 标志。 --int --type=int (及其交换对)不会抱怨,但 --bool --type=int (及其交换对)会抱怨。

因此,在您拥有 --bool--int 之前,现在 (documentation):

--type <type>

'git config' 将确保任何输入或输出在给定的类型约束下都是有效的,并将规范化 规范形式的传出值。有效的 包括: 'bool':将值规范化为“true”或“false”。 'int':将值规范化为简单的十进制数。可选的后缀“k”、“m”或“g”将导致输入时值乘以 1024、1048576 或 1073741824。 “bool-or-int”:如上所述,根据“bool”或“int”进行规范化。 'path':通过在 $HOME 的值中添加前导 ~ 并将 ~user 添加到指定用户的主目录来规范化。此说明符在设置值时无效(但您可以从命令行使用 git config section.variable ~/ 让您的 shell 进行扩展。)'expiry-date':通过从固定或相对日期转换来规范化-字符串到时间戳。此说明符在设置值时无效。

--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=] 优于 --get-color (但请注意,--get-color 将省略 --type=color 打印的尾随换行符)。

您可以看到 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。


G
Glenn Slayden

参考这里: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

N
Nayana Chandran

假设您希望当前分支为黄色,所有其他分支为青色粗体。我正在考虑您希望这些更改在本地完成,即在您当前的存储库中完成,而不是针对您系统中存在的所有存储库。使用“cd .git”进入 .git 文件,然后打开“config”文件。在配置文件中键入以下内容,而不更改配置文件中的任何其他内容。

 [color]
         ui=true
    [color "branch"]
         local=cyan bold 
         current=yellow bold

然后保存配置文件。打开 git 控制台并执行 git 分支。你会看到区别