5

I'd like to git diff and combine the regular line-by-line diff with git diff --word-diff. The problem with line-by-line diffs is that they're unnecessary if I change one or two words and leave the line mostly intact--the chunking is too coarse. On the other hand if I change entire lines and use --word-diff, sometimes the diff algorithm will get confused and spit out incredibly confusing diffs, with lots of words inserted and deleted to "morph" one line into another.

Is there a way to specify that git should be smart about this and only --word-diff if it actually makes sense to do so (on a line-by-line basis, of course)?

Joseph K. Strauss
  • 4,683
  • 1
  • 23
  • 40
PythonNut
  • 6,182
  • 1
  • 25
  • 41
  • 2
    Did you configure `diff.wordRegex` or use `--word-diff-regex`? I currently use `[A-Za-z0-9_]+|[^A-Za-z0-9_]`, which works excellently for incremental changes, but for full rewrites it goes crazy. I was wondering the same thing. Maybe there is some regex that can accomplish this, but I have not figured it out yet. – Joseph K. Strauss Jan 15 '15 at 22:36
  • 2
    Hi @JosephK.Strauss, after reviewing your edit and noticing it was accepted by the OP, I took a closer look at the question. Note that myself and one other reviewer voted to reject your edit because there is no indication in the question or the edit description that it needs to be categorized [regex]. Now, seeing your comment, it's more clear that the tag could be applicable. In the future it's a good idea to be detailed in the summary of your edit suggestion, as the comments are not visible to us when reviewing. – Air Jan 15 '15 at 23:30

1 Answers1

5

The smartest thing I have found for git diff --word-diff or git diff --color-words are the predefined patterns that come with git (as used in --word-diff-regex or diff.wordregex). They might not be perfect, but give quite good results AFAICT.

A list of predefined diff drivers (they all have predefined word regexes too) is given in the docs for .gitattributes. It is further stated that

you still need to enable this with the attribute mechanism, via .gitattributes

So to activate the python pattern for all *.py files, you could issue the following command in your repo root:

echo "*.py diff=python" >> .gitattributes

If you are interested in what the different preset patterns actually look like, take a look at git's source code

raphinesse
  • 19,068
  • 6
  • 39
  • 48