Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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);

  • when running git diff it shows the paragraph before in red and paragraph after in red, making it hard to spot the difference
  • when running git diff --word-diff, it shows the paragraph after in gray and doesn't show the whitespace changes
  • when running git diff --word-diff-regex=. it shows the whitespace changes (great!) but [EDIT] it does character by character diff which is often unreadable as it mixes letters from different words to minimize the diff, eg: git show --word-diff-regex=. 4a720394bba39ce1e67d518b909cbb1c25f63d09 [- * patch compile-]r [-so `isM-]a[-inModule`-] [-is true when -d:isMainModuleIsAlwaysTr-]{+m+}u[-e-] [- T-]{+c+}h[-at'll give speedup-] be[-nefi-]t[-, and we don'-]t[- hav-]e{+r+} [-to p-]{+w+}a[-tch stdlib files-]{+y+}. ]#

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

like image 425
timotheecour Avatar asked Aug 31 '18 23:08

timotheecour


People also ask

How do you tell the difference in files in git?

You can run the git diff HEAD command to compare the both staged and unstaged changes with your last commit. You can also run the git diff <branch_name1> <branch_name2> command to compare the changes from the first branch with changes from the second branch.

Which command will show the differences between the staged and committed versions of the file?

The git diff command shows the differences between the files in two commits or between your current repository and a previous commit.

What is the difference between the git diff and git status?

The main difference between the commands is that git diff is specially aimed at comparisons, and it's very powerful at that: It can compare commits, branches, a single file across revisions or branches, etc. On the other hand, git status is specifically for the status of the working tree.

Why is git diff not showing?

Why do you get no git diff output before adding? Git does not treat files in the filesystem as automatically included in the version control system. You have to add things explicitly into the Git repository (as you are doing by adding the current directory with git add . ).


1 Answers

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

like image 196
Silveri Avatar answered Oct 08 '22 00:10

Silveri