Last Updated: February 25, 2016
·
656
· dagams

Compare current version of a file to old git revision

I've written this little script the other day when I needed to revert some changes I had made in an earlier commit, but I didn't want to revert the entire file. I also had some local changes to the file, so really what I needed was a way to view the current version of the file next to the version in the old commit. My tool of choice for file comparison is TextWrangler, but you might prefer FileMerge or Kaleidoscope. Here's a "quick and dirty" shell script to do just that:

#!/bin/bash
FILENAME=$(basename "$2")
EXT="${FILENAME##*.}"
TMPFILE=$(mktemp -t tmp).$EXT
git show $1:$2 > $TMPFILE
twdiff --wait $2 $TMPFILE # replace this with the diff tool of your choice
rm -f $TMPFILE

you invoke it by calling diff_old_to_new.sh <tree-ish> <filename>, where tree-ish is any commit reference that git understands (ie HEAD or the full SHA)