86

I'm trying to set up pretty format colors for Git. From what I can tell version 1.6.0 only recognizes red, green and blue.

$ git log --pretty=format:"%Credred%Creset %Cgreengreen%Creset %Cyellowyellow%Creset %Cblueblue%Creset %Cmagentamagenta%Creset %Ccyancyan%Creset %Cwhitewhite%Creset"
red green %Cyellowyellow%Creset blue %Cmagentamagenta %Ccyancyan %Cwhitewhite

In addition none of the colors work with the parenthesized color format.

Is there a way to list the available pretty format colors for Git?

Unfortunately this is on a legacy SCO OpenServer 5.0.7 machine and the last version of Git released by SCO Skunkworks was 1.6.0.3.

James Allman
  • 38,410
  • 9
  • 53
  • 69
  • 1
    If your platform is obsolete, why don't you download latest git 1.8.x from git-scm.com and compile it yourself? Then it would support all the most recent features. – mvp Mar 17 '13 at 06:50
  • @mvp Well, as you said, the platform is obsolete - meaning a lack of a GNU build environment. I'm preparing the existing application source code for migration to Linux. – James Allman Mar 17 '13 at 16:53
  • Note that Git 2.3.0 (February 2015) will allow for 24-bits color values, and a better management of color attributes (bold, underline, ...). See [my answer below](http://stackoverflow.com/a/28256977/6309) – VonC Jan 31 '15 at 21:58
  • Git 2.26 (Q1 2020) will add the attribute "bright" to support bright aixterm colors. See [my edited answer below](http://stackoverflow.com/a/28256977/6309) – VonC Feb 26 '20 at 07:33

5 Answers5

148

I do not have an old version of git to verify that the colors other than red, blue and green are supported.

Although, one thing I noticed even with the recent versions of git (like 1.7.10 I used) is that colors other than red, green and blue need to be within parentheses (). For red, green and blue, the parentheses are optional.

So give this a try:

git log --pretty=format:"%Credred%Creset %Cgreengreen%Creset %C(Yellow)yellow%Creset %Cblueblue%Creset %C(magenta)magenta%Creset %C(cyan)cyan%Creset %C(white)white%Creset"

The list of colors I'm aware of at least are:

normal
black
red
green
yellow
blue
magenta
cyan
white

It can be combined with one of these attributes:

bold
dim
ul
blink
reverse
italic
strike
bright  # (Git 2.26, Q1 2020, example: brightred)

If you're trying to change colors using .gitconfig you should be able to specify two colors - foreground and background and you can combine it with an attribute.

VonC
  • 1,042,979
  • 435
  • 3,649
  • 4,283
Tuxdude
  • 40,779
  • 13
  • 96
  • 102
  • I believe you are right in that only the primary colors are supported. The parenthesized color format does not work in this version (1.6.0.3) at all. – James Allman Mar 17 '13 at 17:02
  • 16
    space seperate foreground, background & attributes like this %C(red green bold) – HexInteractive Oct 16 '13 at 18:32
  • There are at least two other attributes: **italic** and **strike** as written in the manual here https://git-scm.com/docs/git-config and for completness reasons. – onemorequestion May 15 '20 at 20:37
  • 1
    @onemorequestion No problem. I have edited the answer accordingly. I really liked your other answer (https://stackoverflow.com/a/61487052/6309) by the way. – VonC May 15 '20 at 21:04
41

Git 2.3.0 (February 2015) will allow (thanks to Jeff Kink (peff)):


parse_color: support 24-bit RGB values

Some terminals (like XTerm) allow full 24-bit RGB color specifications using an extension to the regular ANSI color scheme.
Let's allow users to specify hex RGB colors, enabling the all-important feature of hot pink ref decorations:

git log --format="%h%C(#ff69b4)%d%C(reset) %s"

  • a better management of color attributes:

parse_color: recognize "no$foo" to clear the $foo attribute

You can turn on ANSI text attributes like "reverse" by putting "reverse" in your color spec. However, you cannot ask to turn reverse off.

For common cases, this does not matter. You would turn on "reverse" at the start of a colored section, and then clear all attributes with a "reset".

However, you may wish to turn on some attributes, then selectively disable others. For example:

git log --format="%C(bold ul yellow)%h%C(noul) %s"

underlines just the hash, but without the need to re-specify the rest of the attributes.

This can also help third-party programs, like contrib/diff-highlight, that want to turn some attribute on/off without disrupting existing coloring.

Note that some attribute specifications are probably nonsensical (e.g., "bold nobold"). We do not bother to flag such constructs, and instead let the terminal sort it out.


With Git 2.26 (Q1 2020), the basic 7 colors learned the brighter counterparts (e.g. "brightred").

See commit c444f03, commit 1751b09, commit 4a28eb0 (21 Jan 2020) by Eyal Soha (``).
(Merged by Junio C Hamano -- gitster -- in commit 87f17d7, 25 Feb 2020)

color.c: support bright aixterm colors

Signed-off-by: Eyal Soha

These colors are the bright variants of the 3-bit colors.

Instead of 30-37 range for the foreground and 40-47 range for the background, they live in 90-97 and 100-107 range, respectively.

The git config documentation now includes:

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.

VonC
  • 1,042,979
  • 435
  • 3,649
  • 4,283
  • See also http://stackoverflow.com/a/12798205/6309: git 2.9.1 supports two new attributes: *italic* and strike. – VonC Jul 12 '16 at 13:00
  • 2
    Do you know how to include the RGB colours inside a git alias? `#ff69b4` works on the command line but not in an alias, because `#` starts a comment – Tim Malone Jul 15 '16 at 00:53
  • @TimMalone I didn't test it yet, but the config man page says "Leading whitespaces after name `=`, the remainder of the line after the first comment character `#` or `;`, and trailing whitespaces of the line are discarded **unless they are enclosed in double quotes**. " – VonC Jul 15 '16 at 07:15
  • 1
    @TimMalone Put the whole alias in `"`-quotes and then the `#` won't be special. – Tom Hale Oct 06 '16 at 08:16
  • @VonC, I get weird characters when I pipe those `git log` command outputs to a text or clip. It looks like this happens when colors and attributes are used with `--format`. Any idea why or fix for this? – hIpPy Dec 27 '16 at 02:56
  • @hIpPy No idea just yet: could you detail your issue in a new question (with the OS used, Git version used and shell used) – VonC Dec 27 '16 at 08:09
7

Share my git log format:

$ git log --graph --pretty=format:'%C(magenta)%h%Creset -%C(red)%d%Creset %s %C(dim green)(%cr) %C(cyan)<%an>%Creset' --abbrev-commit

You can also add an alias into ~/.gitconfig:

[alias]
    logs  = log --graph --pretty=format:'%C(magenta)%h%Creset -%C(red)%d%Creset %s %C(dim green)(%cr) %C(cyan)<%an>%Creset' --abbrev-commit

then you can use alias as you define in ~/.gitconfig.

$ git logs
Willem Van Onsem
  • 321,217
  • 26
  • 295
  • 405
Xiaofei HAN
  • 129
  • 1
  • 1
4

I am using Ubuntu 18.04 and I was able to use Xiaofei HAN's suggestions.

For beginner trying to complete this, I had the best luck by editing the .gitconfig.

nano ~/.gitconfig

paste [alias] block from above under the existing text

CTRL + O (to write out) + CTRL + X (to close nano)

After doing this, to see the pretty colors you can use

git logs

using

'git log' will get you the view without the colors,

same for 'git logs --oneline', except that one will show the flourish on the side

thank you, i hope this helps someone

daniela-idara
  • 88
  • 2
  • 11
4

Here are two aliases that I wrote once I understood the syntax of using custom hexadecimal color values as shown in @VonC 's answer.

There are four versions, the only difference is the --graph directive and the 'f' versions will also show you files that were added, deleted or modified:

[alias]
    lg = log --graph --pretty=format:"%C(#cd9a00)%h\\%C(#0080ff)\\ <%an>\\ %C(#17b062)(%cr)\\ %d\\%C(#c0d6de)%s"    
    l1 = log --pretty=format:"%C(#cd9a00)%h\\%C(#0080ff)\\ <%an>\\ %C(#17b062)(%cr)\\ %d\\%C(#c0d6de)%s"    
    lgf = log --name-status --graph --pretty=format:"%C(#cd9a00)%h\\%C(#0080ff)\\ <%an>\\ %C(#17b062)(%cr)\\ %d\\%C(#c0d6de)%s" 
    l1f = log --name-status --pretty=format:"%C(#cd9a00)%h\\%C(#0080ff)\\ <%an>\\ %C(#17b062)(%cr)\\ %d\\%C(#c0d6de)%s"

I used this site to pick the exact colors I wanted and then copied the hex value from it:

https://www.colorhexa.com/

For Windows, open up your .gitconfig file that resides in your "C:\Users\YourUserName" Folder and add an alias section as depicted above.

Hope you like them.

ElMatador
  • 517
  • 1
  • 5
  • 15