While I was looking through my ~/.gitconfig
today, I learned some new tricks.
I've had the color
section in my ~/.gitconfig
without really understanding it:
First, it seems that git config color.ui true
is superfluous as it has been set to true
by default since Git 1.8.4. More importantly, I learned that you can customize the colors of git-branch
, git-diff
, git-status
, git log --decorate
, git-grep
, and git {add,clean} --interactive
The easiest way I could find to view what properties are available to colorize is to search for <slot>
on the git-config man page.
The other neat thing I learned is that you can include other config files by adding an include
section in your ~/.gitconfig
:
By combining these two tricks, you can create one colorscheme for a terminal that uses the Tomorrow Theme and one for the terminal that uses Solarized and swap between them easily by swapping the symlink pointed at by ~/.config/git/color.config
# Tomorrow Night Eighties in ~/.config/git/tomorrow-night-eighties.config
# Included in ~/.gitconfig via:
# [include]
# path = ~tyler/.config/git/tomorrow-night-eighties.config
[color "status"]
header = "#999999"
added = "#99cc99"
changed = "#f2777a"
untracked = "#ffcc66"
branch = "#2d2d2d" "#6699cc"
# Because the phrase "Detached HEAD" isn't unnerving enough
nobranch = bold ul blink "#f99157"
[color "diff"]
meta = "#515151"
func = "#cc99cc"
frag = "#66cccc"
context = "#999999"
old = "#f2777a" "#393939"
new = "#bef2be" "#515151"
I wish the symlink swap didn't have to happen – I wish you could just use environment variables.
As far as I can tell the only variables that are expanded in ~/.gitconfig
are ~
to point at the current user's home directory and ~[user]
points to [user]
's home directory.
include
also fails silently by design.
I feel like there are some other neat tricks to uncover here, but I haven't quite grokked this to fullness just yet.
You can simply pass ANSI color code indices as the color arguments. These indices are to a color palette which is very easily changed from within the settings of your terminal or terminal emulator (effectively giving all color-aware console applications support for psuedocolors). Here is a post with more information on Git 256 color support. For examples of how to do this on various Windows terminals see this stackoverflow post, though it is also possible on Linux and Mac OSX or really any platform with an even remotely modern terminal. This method is superior in ease-of-use as well as portability. View this Imgur gallery for several pictures of various color schemes being dynamically changed within my terminal.
It is important to point out that ANSI color code indices are laid out in a specific way such that certain indices usually correspond to certain usages or hues. This means that, for example, using the color index of 0 is usually the “default background color” of any given theme. Here is a resource which explains the ANSI 256 colors mode escape sequences (note that we are only providing the palette index to Git and don’t need to worry about directly writing an ANSI escape sequence!)
Here is the Git configuration I am using to generate these indexed color codes within diffs:
Note: I “translated” the posted Tomorrow Night Eighties color theme file from the original post into this format as closely as possible without making changes to my terminals built-in color themes. As a result several of the hex code colors have been modified to a closely matching color code index for my terminal’s version of the 16 colors for the Tomorrow Night Eighties theme.