Git smart line and word diff
I would like to git diff
and combine the regular phased distinction with git diff --word-diff
. The problem with linear differences is that they are not needed if I swap one or two words and leave the line mostly intact - the alternation is too rough. 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 differences, with lots of words inserted and deleted to "morph" one line into another.
Is there a way to indicate that it git
should be smart about this and only --word-diff
if it really makes sense to do it (step by step, of course)?
source to share
The smartest I've found for git diff --word-diff
or git diff --color-words
is the predefined templates that come with git (as used in --word-diff-regex
or diff.wordregex
). They give good AFAICT results.
For a list of predefined diff drivers (they all also have predefined text expressions), see the docs for.gitattributes
. It is further indicated that
you still need to enable this with the attribute mechanism via .gitattributes
So, to activate the pattern python
for all files *.py
, you can run the following command in your repo root (omit the quotes under Windows):
echo '*.py diff=python' >> .gitattributes
If you are interested in what the different preset templates look like, have a look at the git source code
source to share