git diff displays colors incorrectly
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
.
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
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.