Skip to content
Advertisement

Pipe in ZSH is stripping colors from Git log output

I’m trying to utilize GRB’s git helpers (https://github.com/garybernhardt/dotfiles/blob/master/.githelpers) for pretty printing my git log.

The git log --graph --pretty="tformat:${LOG_FORMAT}" command from line #62 works just fine; printing with colors in iTerm. But as soon as it’s piped to pretty_git_format there are no colors displayed.

This has been working for several years until just a few weeks ago. Was there a change in zsh that I now need to configure differently?

I’m using iTerm2 and zsh version zsh 5.4.2 (x86_64-apple-darwin16.7.0).

Advertisement

Answer

The default behavior for git is to produce color if the output is being printed to a terminal (directly or through a git-spawned pager). When your output goes somewhere else, like a pipe, git turns off colors.

You can set the color.ui option to always on the command line like this: git -c color.ui=always log --graph --pretty="tformat:${LOG_FORMAT}" (and yes, that’s where the -c option goes). If you want to do this frequently, it can be done with a shell alias in .gitconfig.

While you can set this in your .gitconfig as well, you probably don’t want to. Most external programs assume color is turned off, and you can break other parts of git, as well as tools like editor integrations, if you set this in .gitconfig.

User contributions licensed under: CC BY-SA
7 People found this is helpful
Advertisement