Search code examples
gitcolorspagerless-unix

How to retain color when piping `git difftool` to pager


I cannot get:

git difftool -b -y -x "diff --color --suppress-common-lines -y -w -W 200" master:file.txt file.txt

to retain its coloured output when piping to less -R. What am I doing wrong?

I think I've tried all the git color config options, e.g.

git -c color.ui=always difftool --color=always -b -y -x "diff --color --suppress-common-lines -y -w -W 200" master:file.txt file.txt | less -R

But there's no color. As soon as I remove the pipe to less -R, I see coloured output. Any ideas?

Edit: Thanks to @jonathan-wakely, I was missing the =always argument to the --color option in the external difftool diff command. This now keeps the colors when piping to less:

git difftool -b -y -x "/usr/local/bin/diff --color=always --suppress-common-lines -y -w -W 200" master:file.txt file.txt

Solution

  • The colours are not coming from git they're coming from the external diff tool, so that's what decides whether to use colours or not. So you need to use -x "diff --color=always ..." to tell diff to use colours even when the output is not going to the terminal.