Git Tip: Use Word Diff

Git is great (once you get beyond the basic recipes), and I love my new blog setup that allows me to keep track of all the changes I make with Git.

However, there’s a slight gotcha if you use Git with Markdown: whenever you change something, the whole line (and using tools like IA Writer a whole paragraph is a single line) is marked as changed, for example:

Default git diff printout on a Markdown change

Default git diff printout on a Markdown change

Git has an interesting feature for scenarios like this: the word-diff option that tries to compare words not lines. Using that option on a Markdown text gives you more meaningful results:

Using word-diff option

Using word-diff option

We’re almost there. Git has identified the part of the text that has been changed, but did not correctly identify the actual change made because it considers only whitespaces to be word delimiters, so it seems like I changed (available to (parts....

Changing the definition of what words are with word-diff-regex option fixes that and identifies the actual change I made to the text.

Changing what a word is with word-diff-regex option

Changing what a word is with word-diff-regex option

3 comments:

  1. Nice trick! Thanks Ivan. A follow-up question would be:

    Is it possible to somehow persist such setting, either for your current repo, or ideally as a global setting for .md files across all your repos? This is supposed to be a reasonable need, yet it seems more difficult than I thought. By any chance, have you ever figured that out already?

  2. Haven't found anything along those lines, but don't let that stop you. Git is open-source software, so you can either fork it or fix it and submit a patch.

    See https://github.com/git/git/blob/master/Documentation/SubmittingPatches for details.

  3. @Ray: found something usable ;) In .gitconfig:

    [alias]
    wdiff = diff --word-diff --word-diff-regex='\\w+'
    
Add comment
Sidebar