46

In order to get coloured output from all git commands, I set the following:

git config --global color.ui true

However, this produces an output like this for git diff, git log

git diff screenshot

whereas commands like git status display fine

git status screenshot

Why is it not recognizing the escaped color codes in only some of the commands and how can I fix it?

I'm using iTerm 2 (terminal type xterm-256color) on OS X 10.8.2 and zsh as my shell

zsh --version
zsh 5.0.0 (x86_64-apple-darwin12.0.0)

git --version                                                                                                                      
git version 1.7.9.6 (Apple Git-31.1)
65

You're seeing the escape sequences that tell the terminal to change colors displayed with the escape character shown as ESC, whereas the desired behavior would be that the escape sequences have their intended effect.

Commands such as git diff and git log pipe their output into a pager, less by default. Git tries to tell less to allow control characters to have their control effect, but this isn't working for you.

If less is your pager but you have the environment variable LESS set to a value that doesn't include -r or -R, git is unable to tell less to display colors. It normally passes LESS=-FRSX, but not if LESS is already set in the environment. A fix is to explicitly pass the -R option to tell less to display colors when invoked by git:

git config --global core.pager 'less -R'

If less isn't your pager, either switch to less or figure out how to make your pager display colors.

If you don't want git to display colors when it's invoking a pager, set color.ui to auto instead of true.

  • Ah, thanks. While I knew that less needed -R to display colors, I forgot that git log and git diff piped output to less. Thanks :) – user32465 Feb 16 '13 at 1:10
  • 5
    Thank you! I hadn't noticed that the default ~/.profile in FreeBSD had PAGER=more; export PAGER – Jeffrey Goldberg Mar 3 '14 at 22:26
  • Thanks. I had LESS set to -K because I like being able to CTRL-C everything. I thought it was something funky with that variable, and now I know how to fix it. – trysis Apr 3 '16 at 13:24
2

Improving upon Gilles' answer, you can keep your old $LESS options by using this in your git config (for example in ~/.gitconfig):

[core]
pager = less -+$LESS -FRX
  • quazgar's answer is good. I have LESS=-i in my bashrc, So I would have to do ` [core]pager = less $LESS -FR` I took out the X, because with X, my existing terminal content gets erased, I prefer that the terminal content stays. – Wayne Walker Sep 2 at 20:58
1

You could also see this output if you have Busybox. Alpine Linux is a good example of a popular distro that uses it. See https://wiki.alpinelinux.org/wiki/Alpine_Linux:FAQ#How_to_enable.2Ffix_colors_for_git.3F

A simple way to know if you currently use Busybox is to run:

realpath `which less`

Or if you use fish shell

realpath (which less)

If the output is /bin/busybox, then you can solve your problem by installing gnu less.

On Alpine:

sudo apk --update add less

The command will be different depending on you distro / package manager.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy