Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Git pretty format colors

Tags:

git

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.

like image 282
James Allman Avatar asked Mar 17 '13 06:03

James Allman


People also ask

What is pretty format in git?

<full-commit-message> reference. <abbrev-hash> (<title-line>, <short-author-date>) This format is used to refer to another commit in a commit message and is the same as --pretty='format:%C(auto)%h (%s, %ad)' . By default, the date is formatted with --date=short unless another --date option is explicitly specified.

How do I make my git log pretty?

A simple fix is to pass the --pretty=oneline parameter, which makes it all fit on a single line. It's taking up less space, but missing crucial information like the date of the commit. There are longer versions of that same --pretty parameter. In fact, it allows you to specify all the fields you want in the output.

What is git Shortlog?

The git shortlog command is a special version of git log intended for creating release announcements. It groups each commit by author and displays the first line of each commit message. This is an easy way to see who's been working on what.


2 Answers

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.

like image 167
Tuxdude Avatar answered Oct 06 '22 13:10

Tuxdude


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


  • 24-bits RGB color values (commit 17a4be2)

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.


With Git 2.35 (Q1 2022), "default" and "reset" colors have been added to our palette.

See commit de65851 (26 Oct 2021), and commit 05f1f41, commit aeefc18 (25 Oct 2021) by Robert Estelle (rwe).
(Merged by Junio C Hamano -- gitster -- in commit 15209c8, 15 Dec 2021)

color: allow colors to be prefixed with "reset"

Signed-off-by: Robert Estelle

"reset" was previously treated as a standalone special color name representing \e[m.
Now, it can apply to other color properties, allowing exact specifications without implicit attribute inheritance.

For example, "reset green" now renders \e[;32m, which is interpreted as "reset everything; then set foreground to green".
This means the background and other attributes are also reset to their defaults.

Previously, this was impossible to represent in a single color: "reset" could be specified alone, or a color with attributes, but some thing like clearing a background color were impossible.

There is a separate change that introduces the "default" color name to assist with that, but even then, the above could only to be represented by explicitly disabling each of the attributes: green default no-bold no-dim no-italic no-ul no-blink no-reverse no-strike

config now includes in its man page:

The pseudo-attribute reset resets all colors and attributes before applying the specified coloring. For example, reset green will result in a green foreground and default background without any active attributes.

like image 27
VonC Avatar answered Oct 06 '22 14:10

VonC