39

When I run git diff, the header part of each diff comes out in white text. Since I'm using a light background it is hard to read, so I want to change it.

I have found that I can change other colors in the diff output like this (in .gitconfig):

[color "diff"]
    old = green
    new = red

But I can't figure out what to put there for the header color. Even better, is there someplace where all of the default git config settings are documented?

By 'diff header' I mean lines like this:

diff --git a/README.md b/README.md
index f102026..c5e3428 100644
--- a/README.md
+++ b/README.md

4 Answers 4

45

Try setting color.diff.meta, e.g.

git config --global color.diff.meta blue

or by manually editing the configuration file

[color "diff"]
    meta = blue

You can look through the various color. settings in the git-config reference for more possible settings. The color.diff.meta setting is listed here:

color.diff.<slot>
Use customized color for diff colorization. <slot> specifies which part of the patch to use the specified color, and is one of plain (context text), meta (metainformation), frag (hunk header), func (function in hunk header), old (removed lines), new (added lines), commit (commit headers), or whitespace (highlighting whitespace errors). The values of these variables may be specified as in color.branch.<slot>.

3
  • Good answer, but I wanted to change the colors of new lines and old (deleted) lines too, so I did the research and expanded upon your answer here: stackoverflow.com/a/61993060/4561887 Commented May 24, 2020 at 23:19
  • 1
    Or if you want to avoid having to edit your global config (perhaps for a one off): git -c color.diff.meta=blue diff <file1> <file2>
    – Cole
    Commented Feb 21, 2021 at 5:40
  • Great answer! Color bind guys like me, do: git config --global color.diff.old blue Commented Oct 1, 2022 at 9:06
21

A Google search for "git diff change colors" shows this question as the top hit, so let me add the info I came here looking for, now that I've figured it out, knowing others will land here too looking for the same info.

Basic git diff color options:

  1. meta = header information (white by default), that looks like this:

     diff --git a/home/.bashrc b/home/.bashrc
     index 148212d..a0d16d8 100644
     --- a/home/.bashrc
     +++ b/home/.bashrc
    
  2. old = deleted lines (red by default)

  3. new = added lines (green by default)

Change those colors in your global gitconfig file in ~/.gitconfig like this:

git config --global color.diff.meta blue
git config --global color.diff.old blue
git config --global color.diff.new blue

or by editing ~/.gitconfig directly and adding these lines:

[color "diff"]
    meta = blue
    old = blue
    new = blue

For more color settings, or "slots" you can change, check the man pages:

man git config

or look online here, and search for color.diff.<slot>.

What colors can I use?

See the color section from man git config, or online here: https://git-scm.com/docs/git-config#Documentation/git-config.txt-color

color

The value for a variable that takes a color is a list of colors (at most two, one for foreground and one for background) and attributes (as many as you want), separated by spaces.

The basic colors accepted are normal, black, red, green, yellow, blue, magenta, cyan and white. The first color given is the foreground; the second is the background. All the basic colors except normal have a bright variant that can be specified by prefixing the color with bright, like brightred.

Colors may also be given as numbers between 0 and 255; these use ANSI 256-color mode (but note that not all terminals may support this). If your terminal supports it, you may also specify 24-bit RGB values as hex, like #ff0ab3.

The accepted attributes are bold, dim, ul, blink, reverse, italic, and strike (for crossed-out or "strikethrough" letters). The position of any attributes with respect to the colors (before, after, or in between), doesn’t matter. Specific attributes may be turned off by prefixing them with no or no- (e.g., noreverse, no-ul, etc).

An empty color string produces no color effect at all. This can be used to avoid coloring specific elements without disabling color entirely.

For git’s pre-defined color slots, the attributes are meant to be reset at the beginning of each item in the colored output. So setting color.decorate.branch to black will paint that branch name in a plain black, even if the previous thing on the same output line (e.g. opening parenthesis before the list of branch names in log --decorate output) is set to be painted with bold or some other attribute. However, custom log formats may do more complicated and layered coloring, and the negated forms may be useful there.

More complex example with foreground, background, & text attributes (bold, italic, strike-through):

Here's a more complex example. The first color is the foreground color, the 2nd color is the background color, and any words thereafter are attributes. See the manual pages quoted above for details.

Run these commands:

git config --global color.diff.meta "blue"
git config --global color.diff.old "black red strike"
git config --global color.diff.new "black green italic"
git config --global color.diff.context "yellow bold"

OR copy/paste the following into the bottom of your ~/.gitconfig file:

[color "diff"]
    meta = blue
    old = black red strike
    new = black green italic
    context = yellow bold # context (ie: unchanged lines) text

Here's some sample output of git diffn (git diff with line numbers) with these settings. Notice the red strikethrough text for deleted lines. Pretty cool. I didn't know this was possible until today (not that I like these colors though--I think the default is best :)).

Also notice that the colons are NOT colored or stylized to match the surrounding text on the left and right. This is intentional and designed-in behavior to act as a visual separator between the line numbers added on the left and the original git diff output on the right.

enter image description here

4
  • 4
    If you want to avoid changing your global config (ie you just want the colors changed for a one-off) you can pass the parameter via -c eg: git -c color.diff.new=cyan diff <file1> <file2> shows all additions in cyan
    – Cole
    Commented Feb 17, 2021 at 3:14
  • 1
    Nice, simple way to custom color your terminal anytime Commented Feb 20, 2021 at 21:21
  • 2
    @jasonleonhard Yep, I also forgot to add for complex specifiers you just need to add quotes. eg for bold cyan: git -c color.diff.new="cyan bold" diff <file1> <file2>
    – Cole
    Commented Feb 21, 2021 at 5:37
  • @caot, what did you try exactly? Commented May 3, 2021 at 21:44
2

Definitely recommend: diff-so-fancy

"diff-so-fancy strives to make your diffs human readable instead of machine readable. This helps improve code quality and helps you spot defects faster."

2
  • Why? Please add some sort of explanation of advantages.
    – Brian C
    Commented Jul 9, 2021 at 1:34
  • 1
    Added the blerp they provided on their site. The legibility is nice too, high contrast colors. Commented Jul 9, 2021 at 2:06
0

If you are trying this in a Windows environment, you can probably find your global .gitconfig at "C:\Users\username\.gitconfig."

git config --global works, but if you run it more than once, like I did, you will end up with duplicate entries in your .gitconfig. You will probably just want to edit it by hand then.

1
  • Just tested with git 2.30.1 on Mac, and I could not reproduce the "duplicate entries" problem.
    – TextGeek
    Commented Mar 28, 2022 at 16:39

Not the answer you're looking for? Browse other questions tagged or ask your own question.