109

I'm going through a codebase and fixing whitespace oddities and generally correcting indentation and such things, and I want to make sure I haven't inadvertently made any other changes, so I'm doing git diff -w to display differences in all changed files while ignoring whitespace differences. The problem is that this is not actually ignoring all whitespace differences—at least what I consider to be merely whitespace differences. For instance, in the following output from git diff -w,

-"Links":
-{
-
-    "Thermal":
-
-{
-
+  "Links": {
+    "Thermal": {

you can see that I've only

  1. removed superfluous blank lines,
  2. put curly braces on the end of the line of the key whose value they open, and
  3. indented to fit the context

This question looked like it might offer an answer at first, but it deals with differences between two specific files, not between two specific commits. Everything else turned up by searching was a dead end as well. For instance, this question is about merging, not displaying differences, and this question deals with displaying word-level differences, and so forth.

3
  • 2
    For Bitbucket users, there is a proposed fix for this, but it hasn't been coded yet, and probably won't until there is enough interest. You can go to the Bitbucket site and show your support. I initially found the current page while searching for a solution in Bitbucket, so if there are others out there in this situation, please go here and vote! Commented May 2, 2018 at 17:06
  • git difftool + kdiff3 is one option
    – Tim Abell
    Commented Mar 18, 2019 at 14:54
  • 1
    I have been using difftastic for this with success. It compares the ASTs between the two inputs instead of their lines, at the cost of speed and memory usage compared to diff.
    – MHebes
    Commented Jun 14, 2023 at 15:53

3 Answers 3

100

Perhaps there is a better answer, but the best solution I've found so far is this.

First, you must control the definition of "whitespace" that Git is currently using.

git config core.whitespace '-trailing-space,-indent-with-non-tab,-tab-in-indent'

Next, you must control the definition of a word used. Instead of just using git diff -w, add --word-diff-regex='[^[:space:]]':

git diff -w --word-diff-regex='[^[:space:]]'

You'll still see the context, which (in my case, since I'm trying to ensure that there are no differences except whitespace differences) is not helpful. You can use -U0 to tell Git to give you 0 lines of context, like so,

git diff -w -U0 --word-diff-regex='[^[:space:]]'

but you'll still get output that looks pretty much like context, but it's still much better than looking through all the changes carefully and manually to make sure they are only whitespace changes.

You can also do all the above in one command. The -c flag changes git config just for one command.

git -c core.whitespace=-trailing-space,-indent-with-non-tab,-tab-in-indent diff -U0 --word-diff-regex='[^[:space:]]'
6
  • 6
    @Torek - yet another piece of Git goodness. Why in the hell is it so difficult to diff without all whitespace??? I'm on a Windows machine trying to evaluate a PR on Visual Studio project files. I don't care about CR, LF or CRLF differences, but that's what Git is filling my terminal with.
    – jww
    Commented Sep 19, 2016 at 19:58
  • Anyway to create an alias for it?
    – StR
    Commented Dec 5, 2018 at 16:22
  • 3
    @StR you can make an alias in .gitconfig with the following in your [alias] section: diffw = diff -w -U0 --word-diff-regex=[^[:space:]]
    – mattwright
    Commented Jan 7, 2019 at 13:47
  • 1
    @NSjonas I got the same error. You need to add quotes. I edited the commands in the answer. Commented Jun 15, 2020 at 17:43
  • 3
    Is it possible to remove {+, +}, [- , and -] characters in the changed lines?
    – alper
    Commented Oct 25, 2022 at 15:08
40

Ignore all whitespace changes with git-diff between commits

This question looked like it might offer an answer at first, but it deals with differences between two specific files, not between two specific commits. Everything else turned up by searching was a dead end as well....

I think you are having trouble because Git is the wrong tool for the job. It does not make the task easy, and yielding to accommodate the tool is the wrong approach. Tools are supposed to work for you and not vice versa.

Perform a second clone, and then checkout the starting revision in question. Then run regular diff on them using your current revision: diff -bur --ignore-all-space <dir1> <dir2>.

Here are some of the options for diff

-i, --ignore-case
       ignore case differences in file contents

-E, --ignore-tab-expansion
       ignore changes due to tab expansion

-Z, --ignore-trailing-space
        ignore white space at line end

-b, --ignore-space-change
       ignore changes in the amount of white space

-w, --ignore-all-space
       ignore all white space

-B, --ignore-blank-lines
       ignore changes where lines are all blank
4
  • 4
    None of these options help in this case: diff still treats a line break as different from not having a line break. Commented Sep 21, 2018 at 13:21
  • 1
    with latest git (v2.20.1): error: invalid option: -Z
    – Morteza
    Commented Jan 16, 2019 at 9:48
  • 7
    @MortezaZiaeemehr the option are for the diff command, not the git command. This answer recommends using the diff tool, and not using git for the job at hand.
    – saraf
    Commented Mar 26, 2019 at 9:14
  • 2
    Also had issues with invalid option. --ignore-all-space and --ignore-cr-at-eol worked for me.
    – mix3d
    Commented Jul 15, 2019 at 22:59
1

The tool description of classical diff is "Compare FILES line by line."

And git diff follows that principle as well, although apparently with enhanced capabilities. But the notion of a line still seems fundamental to that tool. I haven't seen a way to work around that. And it is usually not worth the hassle to try and work against the nature of the tool.

In a situation such as described in the question, one would need to tokenize and normalize the text for both pre and post edit revisions of the files in such a way as to end up with handy units of compare for diff, which traditionally are lines, and then run your diff tool of choice on these normalized versions.

Not the answer you're looking for? Browse other questions tagged or ask your own question.