timotheecour
timotheecour

Reputation: 3691

how to show whitespace differences with git --word-diff?

To illustrate the problem: see diff

the only diff in this paragraph (starting with A macro that needs is whitespace differences (newlines inserted / removed in certain places);

What I want is an option to show whitespace differences while running --word-diff (or --word-diff-regex), eg via {+ +} and [- -]; Note: for --word-diff=color would be nice to show these to, eg also via {+ +} and [- -] since otherwise these would disappear.

Note: I'm using colors in my gitconfig.

Note: this doesn't help since whitespace differences are not shown in output of git diff --word-diff=porcelain

Upvotes: 10

Views: 3016

Answers (2)

timotheecour
timotheecour

Reputation: 3691

https://github.com/so-fancy/diff-so-fancy or https://coderwall.com/p/nl-bdg/more-readable-git-word-diff-on-osx may actually be a better approach, showing a github-like diff where more context is shown, but also highlighting the part that actually changed.

Upvotes: 2

Silveri
Silveri

Reputation: 5261

The --word-diff-regex command allows you to specify a regex to customise the --word-diff behaviour. The common example of using a full stop (.) will give a character-by-character match since the full stop regex matches any character. When the regex is not specified the default is different depending on the file type, but usually ignores whitespace changes while also using them as word boundaries.

You could use a regex that divides lines into words as well as white space areas by using something like:

git diff --word-diff-regex="[ ]+|[^ ]+"

With a slightly tweaked version of your linked example, the problem with git diff --word-diff-regex=. can be seen:

enter image description here

While git diff --word-diff-regex="[ ]+|[^ ]+" would give you:

enter image description here

Upvotes: 8

Related Questions