首页 文章

如何为Git控制台着色?

提问于
浏览
349

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

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

9 回答

  • 642

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

  • 7

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

    [color]
      ui = auto
    

    它会处理所有git命令 .

  • 6

    使用Git 2.18,您可以更好地控制在控制台中指定颜色的方式 .
    The "git config" command uses separate options 例如“ --int ", " --bool ”等 to specify what type the caller wants the value to be interpreted as .

    引入了一个新的“ --type=<typename> ”选项,这将使定义新类型更加清晰 .

    commit fb0dc3b(2018年4月18日)和commit 0a8950b(2018年4月9日)Taylor Blau (ttaylorr) .
    (由Junio C Hamano合并 - gitster - 在提交e3e042b,2018年5月8日)

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

    所以在你有 --bool--int 之前,现在(documentation):

    --type <type>
    

    'git config'将确保任何输入或输出在给定的类型约束下有效,并且将以<type>的规范形式规范化传出值 . 有效的<type>包括:'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).
    
  • 26

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

    vim ~/.gitconfig
    

    然后添加:

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

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

    git config --global color.ui true
    
  • 5

    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
    
  • 5

    在Ubuntu或任何其他平台(是的,Windows也是!);从git1.8.4开始,这是released 2013-08-23you won't have to do anything

    许多教程教会用户将“color.ui”设置为“auto”作为设置“user.name/email”以向Git引入自己的第一件事 . 现在变量默认为“auto” .

    所以你会默认看到颜色 .

  • 1

    添加到.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
    
  • 6

    由_5195224_ @VonCcolor.ui 默认为 auto ,因为git 1.8.4 . 不是太快释放;)


    从Unix和Linux Stackexchange问题How to colorize output of git?answer by @Evgeny

    git config --global color.ui auto
    

    color.ui是一个元配置,包含git命令可用的所有各种颜色 . *配置 . 这在git help config中有详细解释 .

    所以基本上它比分别设置不同的 color.* 设置更容易,更具未来性 .

    从中深入解释git config documentation

    color.ui:此变量确定控制每个命令族颜色使用的变量(如color.diff和color.grep)的默认值 . 随着更多命令学习配置以设置--color选项的默认值,其范围将扩展 . 如果您希望所有非用于机器消耗的输出都使用颜色,则将其设置为true;如果您希望此类输出在写入终端时使用颜色,则设置为true;如果您希望git命令不使用颜色,则将其设置为false或never除非使用其他配置或--color选项明确启用 .

相关问题