我最近看到Windows中的git控制台是有颜色的,例如,绿色表示添加,红色表示删除等等。我怎么给git控制台上色呢?

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


当前回答

正如@VonC所指出的,color。ui从Git 1.8.4开始默认为auto


从Unix和Linux Stackexchange问题如何着色输出的git?以及@Evgeny的回答:

git config --global color.ui auto

这颜色。UI是一个元配置,包括所有不同的颜色。*配置可用的git命令。这在git帮助配置中有深入的解释。

基本上,这比设置不同的颜色更简单,更经得起考验。*单独设置。

git配置文档的深入解释:

color.ui: This variable determines the default value for variables such as color.diff and color.grep that control the use of color per command family. Its scope will expand as more commands learn configuration to set a default for the --color option. Set it to always if you want all output not intended for machine consumption to use color, to true or auto if you want such output to use color when written to the terminal, or to false or never if you prefer git commands not to use color unless enabled explicitly with some other configuration or the --color option.

其他回答

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 

好吧,如果你对默认设置不满意,你可以使用ANSI转义代码来帮助你设置颜色,如果你想修改一些文本,你可以编写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"

demo

解释

you can write the long script on .gitconfig use the syntax as below: [alias] your-cmd = !f() { \ \ }; f" echo -n -e (see more echo) -n = Do not output a trailing newline. -e Enable interpretation of the following backslash-escaped \\033[38;2;255;0;0m\\033[4m (see more SGR parameters) \\033[38;2;255;0;0m : 38 mean fore color. 255;0;0 = Red | r;g;b \\033[4m : underline grep : The grep command is used to search text. sed -e 's/be_replace_string/new_string/' replace string to new string.

在你的~/。Gitconfig文件,简单地添加这个:

[color]
  ui = auto

它负责所有的git命令。

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

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

然后保存配置文件。 打开git控制台,执行git分支。 你会发现其中的差别

在Ubuntu或任何其他平台(是的,Windows也是!);从2013年8月23日发布的git1.8.4开始,你不需要做任何事情:

许多教程教用户设置“颜色”。将“user.name/email”设置为“auto”后的第一件事,用于向Git介绍自己。现在该变量默认为"auto"。

所以默认情况下你会看到颜色。