Word-by-word diffs in Git

I personally like all my LaTeX files wrapped at about 80 columns, with rare exceptions. Of course, I track them all with Git. I use Vim, so it’s really easy to rewrap a paragraph whenever I add or delete content from it. The sad thing is, it totally screws up my diffs!

Have you ever tried looking at the history for any page in Wikipedia? It’s beautiful, intuitive, and word-centered. Forget about lines, they’re not suitable for text. Words, however, are.

Example of the word-by-word diff in Wikipedia

Example of the word-by-word diff in Wikipedia

Well, version control systems use the line-by-line diffs, which don’t really help when comparing two versions of a text file. What to do now?

I have googled a bit, and found about wdiff. Seemed interesting, but the output was really ugly. I first thought of using it as an external diff tool in git, but gave up. Nice to know it exists, though.

Then, for some reason, I decided to read the git-diff help one more time and, to my surprise, the feature we want is already there! Introducing to you… git diff --color-words!

Sample output of <code>git diff --color-words</code>

Sample output of git diff --color-words

Amazing, right? Notice how deleted words are in red, added words in green, and the unchanged text surrounding it in white (or black, if you’re not into dark backgrounds). Much more natural and easier to spot differences than the original git diff! But you should be asking yourself “Am I going to have to write this long command every time?”. Certainly not! Git allows you to create aliases, which I just learned about. You can create an alias named wdiff, and the result is a new command, git wdiff, that perfectly fits our needs! Great, isn’t it?

Here’s two ways to do it:

  • Via command-line: git config --global alias.wdiff diff --color-words; or
  • Editing your ~/.gitconfig, add the following lines:
        wdiff = diff --color-words

Piece of cake, right? Hope you liked it, and happy word-diffing! 😎

Special thanks to my friend Carlos Eduardo, who helped me find a good way to illustrate the word-by-word diff!

Update: Better tokenization for LaTeX files

It seems that Git 1.6.2 and earlier now support the use of a regular expression to define what is a word for --color-words. Also, the manpage for the .gitattributes file shows that some filetypes already have useful built-in configurations, including the regular expressions in question, and LaTeX is one of those formats.

In practical terms, this means that \section{Title} can now be seen as several tokens instead of a single word, so renaming the section will provide you with a smarter highlighting.

So, if you don’t have a .gitattributes file yet, you should create one. I just found out about them, and it’s pretty cool. It adds other interesting functionalities such as hints on the diffs for which point in the file (for instance, the LaTeX chapter/(sub)section name) those diffs are taking place. The manpages can tell you more about it, so I’ll keep it short. It’s very easy to enable this, just add the following line to a .gitattributes file in your repository (its syntax is very similar to .gitignore) and everything is set for you:

*.tex    diff=tex

Even if your version is older than 1.6.2, the other benefits are much probably available for you and are well worth it, so give it a try anyway!

19 Responses to “Word-by-word diffs in Git”

  1. Leho Kraav Says:

    holy schmokes, this works wonder for comparing gentoo USE flag changes in make.conf. cheers for the writeup mate (y)

  2. Version control for Gentoo make.conf, USE flags — Leho Kraav 24/7 Says:

    […] Obviously line based diffs don’t do us much good here, since these lines can shift around arbitrarily due to a single use flag change. Enter wdiff which lets us compare USE flags one by one. Eduardo has written a nice piece how to make git wdiff alias. […]

  3. Leo Says:

    What I have always been looking for is for a VCS to **merge** word by word.

    You may have coauthors that wrap the words differently and you don’t want a huge spurious conflict everytime you both edit the same file.

    Does git do that too?

    • Eduardo Says:

      Hi Leo.

      As far as I can tell, git is 100% line-based. However, it does seem to be possible to write your own merge driver or to use a merge tool to aid in resolving conflicts (check the manpages for gitattributes and git-mergetool). It should be possible to use an adequate tool that handles the differences word-by-word at this point. Sadly, to keep the repository clean, you’d need all authors with this setup fully configured, otherwise they may still commit “useless” changes.

      If you happen to stumble upon a solution, please share 🙂

    • cirosantilli Says:

      I’d love to see this too…

  4. Wang Says:

    This is wonderful. I also use git+vim+latex, also like to wrap lines at about 80 characters (I use 78). Thanks for sharing!

    I do have a problem. I use green text on black background. The green color for added text does not show. I guess I will take some time to find out how to change it but if you happen to know, please share.

    • Eduardo Says:

      Hi Wang,

      Colors used by git are configurable using git config or directly editing your ~/.gitconfig file. Take a look at the documentation, as I do not recall the exact options.

      Hope it helps!

  5. Coloring differences at a word-level using gitk at avp::ptr weblog Says:

    […] Please find further interesting thoughts in the discussion of András Salamon on StackExchange and Eduardo’s post. Also Iain Murray’s cwdiff wrapper might be worth taking a closer […]

  6. plaindocs Says:

    Cheers for that. I think you need quotes around the command line version though. Something like
    git config –global alias.wdiff “diff –color-words”

  7. S Says:

    I’d really like to be able to use outside of git, just to diff two files. Would anyone be aware of any way of doing that? (I could add file1 to a git repository and commit it, then move file2 over it and use git diff… but perhaps there is a better way.)

    • S Says:

      Answering my own question: one can install colordiff and dwdiff and use either
      wdiff file1 file2 | colordiff
      or, even better, something like
      dwdiff –color –line-numbers –context=2 file1 file2
      (found via http://noone.org/blog/English/Computer/Debian/CoolTools/dwdiff.html)

      • Anthony Thyssen Says:

        For merging there is an even better method dwfilter. It reformats the first file (into a temporary file) so as to match the word formating used by the second file, and then passes those to the graphical diff program of your choice. I myself like :diffuse”, but you can use anything (xxdiff, k3diff, meld, whatever)… for example

        dwfilter file1 file2 diffuse -w

        Now you can see he word (or block of lines) differences, and with “diffuse” you can edit the second file (as it is unchanged) to merge in the differences from the first file.

        A “-r” option to “dwfilter” will reformat the second file to match the first (so you can edit the first).

        This saves a huge amount of time for me when working with text files, that are often reformatted by different people.

  8. Evan Says:

    For comparing two Tex files, you can try texdiff. Two files are com­pared and a new TEX file is out­put. When the out­put file is pro­cessed with (LA)TEX it marks new changes with blue and old text with red with a strike-through line.

  9. Hong Xu Says:

    “1.6.2 or earlier” — should be “later”

  10. cxw Says:


  11. con-- Says:

    Why is this not the default!? Github uses word level diffs by default, maybe git itself should too?
    So useful, thank you!

  12. git commands simplified – great hornbill Says:

    […] Diffgit diff file-1 file-2few useful diff tools are wDiff: The wdiff utility is a front end to diff command used to compare files on a word by word basismeld […]

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

%d bloggers like this: