AtlassianBlogs Git Diff
AtlassianBlogs Git Diff
| Atlassian Blogs
Atlassian Home Get Help Marketplace
Search Blogs
Subscribe
Subscribe to Developer by
Vive la git diff! email
Your Email
summit 114
Regardless, git has given us a bunch of neat ways at looking at these diffs. The following post is
press releases 109
a quick primer on the various incantations of git diff. Note that Im going to be focusing on
different output format for viewing diffs. Theres also a whole range of ways you can specify plugin 94
different commits and paths to diff, but thats a topic for another day. atlassiantv 93
development tools 83
git diff
wiki 82
Youre probably already familiar with the built in git diffcommand. By default, diff will show video and audio 78
you changes to tracked files in your work tree that havent yet been added to the index. For
buzz 75
example:
$ cat 2cities.txt
Local Blogs
It was the best of times,
it was the blurst of times. Spain Blog
Germany Blog
$ sed -i .tmp s/blurst/worst/ 2cities.txt
Japan Blog
$ git diff
China Blog
It was the best of times,
-it was the blurst of times.
+it was the worst of times.
blogs.atlassian.com/2013/06/git-diff/ 1/5
6/28/13 Vive la git diff! | Atlassian Blogs
This works fine, but its unfortunate that it shows up as one removed line followed by one added
line. All I did was change one word!
Thats an improvement! Now were down to the individual words that have changed. But if you
look closely, weve really only changed three characters: blu has become wo. We can do
even better.
git diff-highlight
If you clone the git source youll find a sub-directory called contrib. It contains a bunch of git-
related tools and other interesting bits and pieces that havent yet been promoted to git core.
One of these is a perl script called diff-highlight. diff-highlight pairs up matching lines of diff
output and highlights sub-word fragments that have changed.
Sweet! Now weve pared down our diff to the smallest possible change.
If you like this fine-grain way of looking at diffs, you can add a shell function alias to
~/.git/configto handle the piping to diff-highlight for you:
1[alias]
2# assumes git.git clone at ~/src/git
3diff-highlight = "!f() { git diff \"$@\" | ~/src/git/contrib/diff-highlight/diff-highlight; }; f"
Then, you can get a nicely highlighted diff by simply invoking git diff-highlight.
$ git diff
However, git does have a nifty feature that allows you to specify a shell command to transform
the content of your binary files into text prior to performing the diff. It does require a little set up
though.
First you need to specify a textconv filter describing how to convert a certain type of binary to
text. Im using a simple utility called pdftohtml (available via homebrew) to convert my PDFs into
human readable HTML. You can set this up for a single repository by editing your .git/config file,
or globally by editing ~/.gitconfig.
.git/config
1[diff "pdfconv"]
blogs.atlassian.com/2013/06/git-diff/ 2/5
6/28/13 Vive la git diff! | Atlassian Blogs
2textconv=pdftohtml -stdout
Then all you need to do is associate one or more file patterns with our pdfconv filter. You can
do this by creating a .gitattributesfile in the root of your repository.
.gitattributes
1*.pdf diff=pdfconv
Then, the next time we attempt to do a diff of a binary file, we get much more meaningful
output:
$ git diff
<HTML>
<BODY>
Interior. Room filled with monkeys bashing on typewriters. Mr. Burns
tears a sheet of paper from a typewriter.<br>
-Burns: It was the best of times.. it was the blurst of times?<br>
+Burns: It was the best of times.. it was the blurst of times? You
stupid monkey!<br>
Burns balls up the sheet and throws it at the monkey.<br>
</BODY>
</HTML>
And remember, this is just regular diff output, so we can redo our diff-highlighttrick to get
something a bit more palatable:
$ git diff-highlight
The same technique can be applied to get useful diffs from all sorts of binary files, for example:
zips, jars and other archives: using unzip -l (or similar) in place of pdf2htmlwill show you
paths that have been added or removed between commits
images: exiv2 can be used to show metadata changes such as image dimensions
documents: conversion tools exist for transforming .odf, .doc and other document formats
to plain text. In a pinch, strings will often work for binary files where no formal converter
exists.
Want to talk more about git or Atlassian Stash? Leave a comment or hit me up on twitter
@kannonboy
Atlassian Summit 2013 - Join us for our annual user conference! $200 off through June
Comments (3)
By default, diff will show you the difference between unstaged changes to currently
tracked files, and your current HEAD.
blogs.atlassian.com/2013/06/git-diff/ 3/5
6/28/13 Vive la git diff! | Atlassian Blogs
Nope!
By default, diff compares the files in your working area with the files in the staging area
(aka the index).
To compare the index with HEAD you can say git diff cached.
Post a Comment
Name(Required)
Email(Required)
Website
Message
blogs.atlassian.com/2013/06/git-diff/ 4/5
6/28/13 Vive la git diff! | Atlassian Blogs
blogs.atlassian.com/2013/06/git-diff/ 5/5