115

I want to colorize git-status output so that:

untracked files = magenta
new files = green
modified files = blue
deleted files = red

I am instead seeing staged files in green and unstaged files in blue: screenshot of git-status

My .gitconfig is setup with the following based on some searching:

[color]
status = auto

[color "status"]
added = green
changed = blue
untracked = magenta
deleted = red
tshepang
  • 12,111
  • 21
  • 91
  • 136
Andy
  • 2,154
  • 3
  • 20
  • 16

1 Answers1

164

From git config doc:

color.status.<slot>

Use customized color for status colorization. <slot> is one of:

  • header (the header text of the status message),
  • added or updated (files which are added but not committed),
  • changed (files which are changed but not added in the index),
  • untracked (files which are not tracked by git),
  • branch (the current branch),
  • nobranch (the color the no branch warning is shown in, defaulting to red),
  • localBranch or remoteBranch (the local and remote branch names, respectively, when branch and tracking information is displayed in the status short-format),
  • unmerged (files which have unmerged changes).

The values of these variables may be specified as in color.branch.<slot>.

So this will work:

git config color.status.changed blue
git config color.status.untracked magenta

However:

new files = green
deleted files = red

Isn't possible: you need to pick one color:

  • if they are added to the index, they will use the color for color.status.added.
  • if they aren't added to the index, they will use the color for color.status.changed.

Note:

Colors may also be given as numbers between 0 and 255; these use ANSI 256-color mode (but not all terminals may support this).
See "xterm 256 colors" for those numbers, as noted in the comments by Joshua Goldberg.


Of course, as commented by elboletaire:

Remember to enable coloring output if it has not been enabled previously:

git config --global color.ui true

Shaun Luttin adds:

The command can also take multiple parameters in quotes. This includes two colors (foreground background) from this list:

normal, black, red, green, yellow, blue, magenta, cyan and white;

and it also includes one attribute (style) from this list:

bold, dim, ul, blink and reverse.

So this will work:

git config color.status.changed "blue normal bold"
git config color.status.header "white normal dim"

Note: with git 2.9.1 (July 2016), The output coloring scheme learned two new attributes, italic and strike, in addition to existing bold, reverse, etc.

See commit 9dc3515, commit 54590a0, commit 5621068, commit df8e472, commit ae989a6, commit adb3356, commit 0111681 (23 Jun 2016) by Jeff King (peff).
(Merged by Junio C Hamano -- gitster -- in commit 3c5de5c, 11 Jul 2016)

It also allow "no-" for negating attributes

Using "no-bold" rather than "nobold" is easier to read and more natural to type (to me, anyway, even though I was the person who introduced "nobold" in the first place). It's easy to allow both.

VonC
  • 1,262,500
  • 529
  • 4,410
  • 5,250
  • 38
    Remember to enable coloring output if it has not been enabled previously: `git config --global color.ui true` – elboletaire Dec 10 '13 at 15:26
  • 1
    @elboletaire good point. I have included it in the answer for more visibility. – VonC Dec 10 '13 at 16:27
  • 1
    Thanks, color.ui is the answer. – Robeezy Dec 11 '13 at 23:44
  • Did you mean to write color.status.changed instead of color.status.modified? Or do both work? – Andy Swift May 27 '21 at 13:19
  • @AndrewSwift 9 years later, reading https://git-scm.com/docs/git-config#Documentation/git-config.txt-colorstatusltslotgt, this should be "changed". I have updated the answer accordingly. Thank you for this comment. – VonC May 27 '21 at 16:10
  • The description of legal color options is at this section of the same document: https://git-scm.com/docs/git-config#Documentation/git-config.txt-color — and here's [a listing of ANSI colors](https://robotmoon.com/256-colors/#table-of-color-codes). (my terminal on OSX doesn't support #ff0088) – Joshua Goldberg Jun 04 '21 at 23:16
  • @JoshuaGoldberg Thank you: I have restored the first link, and included your comment in the answer for more visibility. – VonC Jun 05 '21 at 16:08
  • Can we also redirect the output into `less -r` and keep the color using `git status -sb --ignore-submodules=dirty | less -r` ? – alper Jul 05 '21 at 14:10
  • 4
    @alper Reading https://unix.stackexchange.com/a/19320/7490, I would propose: `git -c color.status=always status -sb --ignore-submodules=dirty | less -rFX` – VonC Jul 05 '21 at 14:55
  • @VonC - so how can I use `ANSI 256-color` codes? I tried: `changed = '\e[38;5;160m'` and `changed = '#d70000'`, but both give me error: `fatal: bad config variable 'color.status.changed'` – 400 the Cat Sep 03 '21 at 02:07
  • @400theCat Maybe your Git version does not support it? Could you ask a separate question, with more details (OS, OS version, git version, shell, ...) – VonC Sep 03 '21 at 06:17
  • @VonC - thank you. I have created new question: https://stackoverflow.com/questions/69112016/how-to-use-ansi-256-color-in-gitconfig – 400 the Cat Sep 09 '21 at 03:51