Color highlighted diffs with git, svn and cvs

4 May, 2009 - 08:57

At first, it seemed annoying, but now I really like that git uses a pager when it has to present something (e.g. a log or a diff) that is longer than your screen. Git even provides a built in option --color to show you a diff with helpful colors. I use this option all the time and made a shortcut dic for it with the following command

git config --global alias.dic "diff --color"

Also possible is to edit your ~/.gitconfig directly so you have something like the following in there:

[alias]
        di = diff
        dic = diff --color

Now I just do

git dic

and I get a nice paged and color highlighted diff.

This is a so I-wonder-how-I-could-live-without-it thingy that I wanted the same when working with Subversion or CVS. These guys don't have color highlighting built in, but luckily there is this standalone "diff colorizer" colordiff to help us out. Colordiff should be available at a sufficiently recent Linux package manager near you (Ubuntu has it since 6.06 for example), otherwise installing it manually is not that hard (it's mainly one perl script and some config files).

If we also add a pager like "less" to the mix, we can mimic the behavior of git. To do so, I added two little scripts to my $PATH.

The first one is cvscolordiff.sh:

#!/bin/sh
cvs diff -bup "$@" | colordiff | less -R

and the one for Subversion is svncolordiff.sh:

#!/bin/sh
svn diff --diff-cmd colordiff -x "-u -w -p" "$@" | less -R

I first tried with constructing bash aliases, but then things go wrong when you try to use arguments to only see diffs of certain files for example. The scripts above take care of this with the "$@" magic, so I can do things like

svncolordiff.sh foo.cpp bar.py

Also note (in case your wondering) the -R option for less. This options makes sure less presents nice colors instead of spitting color control character garbage at you.

17 January, 2012 - 15:40

Thanks for the tips! You

Aultbot (not verified)

Thanks for the tips!

You could create a bash function like so in your ~/.bashrc.
Then you wouldn't need to invoke with .sh or keep it in a seperate script.

function cvsdiff {
cvs diff -bup "$@" | colordiff | less -R
}

4 August, 2011 - 02:29

color diff in git

Tyler Breisacher (not verified)

You can also just do `git config --global color.ui true` to tell git to use colors for pretty much everything, including not just diffs, but also `git status`, `git add -p`, etc.

11 December, 2010 - 03:56

very useful thanks

erq (not verified)

very useful thanks

18 January, 2010 - 10:45

Thanks

Maxe (not verified)

Thank you so much! :)

Post new comment

The content of this field is kept private and will not be shown publicly.
  • No HTML tags allowed
  • Lines and paragraphs break automatically.

More information about formatting options