2

I am trying to set up custom colors for git status, as described here

While using predefined color names works:

[color "status"]
    changed = yellow normal

using ANSI 256 color codes does not work:

[color "status"]
    changed = "\033[38;5;214m" normal

I get an error:

fatal: bad config line 31 in file .gitconfig

I have tries using hex code, but that does not work either:

[color "status"]
    changed = "#d70000" normal

I suspect the # is interpreted as comment.

My git version is 2.20.1

400 the Cat
  • 266
  • 3
  • 23
  • 1
    When I say `git -c color.branch.current='#808080' branch` I get a gray current branch name. What do you get? (note: also built v2.20.1 and tried it again, that worked too). – jthill Sep 09 '21 at 04:54
  • The problem "ANSI 256 colours: are just one of the many ANSI definitions of colours (and ANSI is not about colour, but escape sequence). So what you write is not portable, and it should not be your task to find the codes: you should write it semantically, and let terminal libraries to transform and send correct data into appropriate format. – Giacomo Catenazzi Sep 09 '21 at 08:28

3 Answers3

4

If you want to use a 256-color code, you can do so with a normal number from 0 to 255. For example, in my .gitconfig, there's these entries:

[color.diff]
        new = 34
        old = 203

You don't want to place an actual escape sequence into the file. Git knows how to take a numeric value and issue the proper ANSI escape sequnce. It does not, however, handle terminal-specific capabilities or use terminfo, so if your terminal doesn't support the standard ANSI sequences, then it won't work with Git.

bk2204
  • 64,793
  • 6
  • 84
  • 100
2

First, "" would not be supported in config

changed = #d70000 normal

Second, your terminal has to support it. In a CMD on Windows, for instance, that would not work.

I did not manage to include Ansi escape code though.

VonC
  • 1,262,500
  • 529
  • 4,410
  • 5,250
  • OP's syntax works and is documented to work. This is a subpar or misconfigured terminal or terminal lilbrary. – jthill Sep 09 '21 at 16:00
  • @jthill Possible: it does not work from a Git for Windows bash or CMD. – VonC Sep 09 '21 at 16:14
  • @VonC Is there a way to work that command in Windows git bash? I have windows git bash, in my case, all 256 colors are supported for `[color "branch"]` but don't support for `[color "status"`. – Subrato Pattanaik Jan 22 '22 at 08:16
  • @SubratoPattanaik I tested and did not manage to make it work, even in a W10 terminal console (https://github.com/microsoft/terminal) – VonC Jan 22 '22 at 18:29
-2

From documentation (https://git-scm.com/book/en/v2/Customizing-Git-Git-Configuration):

You can set the color to any of the following values: normal, black, red, green, yellow, blue, magenta, cyan, or white. If you want an attribute like bold in the previous example, you can choose from bold, dim, ul (underline), blink, and reverse (swap foreground and background).

So you have just a limited selection, OTOH it is good to have few colours on terminals (you can change each colour, e.g. solarize the palette, but for usability, less is better).

Note: using names is good: git will find and "outsource" the generation of correct escape sequence according to the terminal of choice.

Giacomo Catenazzi
  • 8,519
  • 2
  • 24
  • 32
  • This is misleading since the documentation also says you can use for example `#ff0000` or ANSI 256 color codes if the terminal supports it. – Ted Lyngmo Jun 14 '23 at 13:54
  • What are the "ANSI 256 color codes"? Please uses just the standard 16 colours: we want readability, not colours you may never distinguish on a screen. 256*256*256 colours are much more then capability of our eyes. And because often our screen are not calibrated, the mess is huge. – Giacomo Catenazzi Jun 14 '23 at 14:45
  • [256-colors-cheat-sheet](https://www.ditig.com/256-colors-cheat-sheet). _"we want..."_ ... well, what _you_ want is not a reason to pretend there aren't other options. I am pretty happy with using most of the basic presets for the 256 colors but need to tweak them on some screens since my eyesight is not top notch anymore. I use `color.diff.old = 203` in the `.gitconfig` that I copy around though. – Ted Lyngmo Jun 14 '23 at 15:10