Since my upgrade to OS X 10.5 I have been having a terrible time getting colors to work with git.
Originally I was using OS X 10.5.4, git version 1.5.3.1, and iTerm or Terminal.app. $TERM=xterm or xterm-color. I have color with PS1, ls, vim and most git tasks. However, git diff (or git show) always screws up the diff by showing the ESC[ characters instead of colors. For instance:
[nathan@nate ~/s3]$ git show 67d254ec17f ESC[33mcommit 67d254ec17fdc507765ddee1feb2a14e5896e79fESC[m
I searched and searched wasn't able to figure out how to fix this. It was suggested on the github forum that I upgrade to git 1.5.6.4. Unfortunately not only did it not fix the color in git diff, it broke the color in git status which was working previously.
After about a week of poking around on and off I discovered that the problem was not git, but my GIT_PAGER which is more. If I tried GIT_PAGER=cat git-diff then the color output just fine. less and more use the same environment variable LESS (man less for details). And less was outputting the colors as raw escape codes.
man less shows that the option -R is defined as follows:
-R or --RAW-CONTROL-CHARS
Like -r, but only ANSI "color" escape
sequences are output in "raw" form.
That would do it.
After perusing the man page for a while I settled on adding this to my
~/.profile:
export LESS="-erX"
At the end of the day, this tip is really about less and how it interprets colors and probably has little to do with git. Even so, I only found this odd behavior while using git. Hopefully this post will help someone who is having similar issues.
As a side note, below is an example of how to configure your colors for git-diff and other commands.
[user]
name = Your Name
email = y...@yours.com
[color]
branch = auto
status = auto
diff = auto
[color "diff"]
meta = yellow
frag = cyan
old = red
new = green
[color "branch"]
current = yellow reverse
local = yellow
remote = green
[color "status"]
added = yellow
changed = green
untracked = cyan
[alias]
st = status
ci = commit
co = checkout
br = branch

12 Comments
Thank you for this post, I’m not using a mac, but this was actually really irritating me when I was xterm’ing into one of my linux boxes from windows.
“After perusing the man page for a while …”
Maybe it’s a bit easier to scan man pages for essential information using:
pman, http://snippets.dzone.com/posts/show/4274
or
openman, http://codesnippets.joyent.com/posts/show/1599
Just a not-quite-on-topic tip!
And thanks for sharing the export LESS=”-erX” hint!
Hi!
Thanks for this Post!
I was experiencing this kind of trouble since I’ve started working on git!
Best Brazilian Regards!
Thank you so much! I was annoyed by “^M” ’s at the end of the diff lines. Now they are gone!
Weird stuff, your post fixed my colors on OpenSuSE at work but strangely enough I have had no problems on my Leopard iMac at home.
Thank you very much! I had exactly the same problem, now is solved. I wish this was documented in git tutorials, although is not directly git related…
Looks like this is fixed under Snow Leopard. I get colours with my config set correctly in ~/.gitconfig and I _did not_ need to have the less switches in my ~/.profile
Hi,
Thanks for your googling and your blog. I was having the same issues on opensuse 11.1.
http://rpg-314.blogspot.com/2009/10/getting-colors-i-git-output-on-opensuse.html
This post helped me fix it. Thanks a lot.
I found using the -r flag did fix git-diff, but it broke something else in less. Specifically, searching in files using less. The token would be found, but some of the matching lines would be hidden. Not very useful, that. I changed the less flag from -r to -R and both git-diff and less search works. Here’s a snippet from the less man page, which is very apropos to git-diff:
-R or –RAW-CONTROL-CHARS
Like -r, but only ANSI “color” escape sequences are output in
“raw” form. Unlike -r, the screen appearance is maintained cor-
rectly in most cases. ANSI “color” escape sequences are
sequences of the form:
ESC [ … m
Thanks for the post Nate and thanks pablitostar for your suggestion. I started using git few days back and was totally annoyed by ESC chars in diff !
Thanx for this blog entry.
Was having this problem while ssh-ing to a linux vps w/Putty. Thought it was a faulty terminal emulation causing the problem. Nope, just the goofy default LESS options in OpenSuSe on the vps. I’ve run linux boxen for years off and on. Never had so much problem with raw ESC character output til it was highlighted by git.
Thanks a lot!! This really proved helpful on opensuse 11.2