When two files "have the same content" yet diff reports lots of changes, whitespace is often the cause: an editor stripped trailing spaces, converted tabs to spaces, or changed the indentation width. diff tools offer options to ignore such differences, but each option ignores a different range of things, so you need to know exactly what each one does.
Whitespace options in GNU diff
| Option | Long name | What it ignores |
|---|---|---|
-Z | --ignore-trailing-space | Whitespace at the end of a line |
-E | --ignore-tab-expansion | Differences due to tabs expanded into spaces |
-b | --ignore-space-change | Changes in the amount of whitespace, trailing whitespace |
-w | --ignore-all-space | All whitespace |
-B | --ignore-blank-lines | Changes that only add or delete blank lines |
-i | --ignore-case | Letter case |
--strip-trailing-cr | CR characters at the end of input lines |
The corresponding git diff options
The git documentation describes them as follows.
--ignore-cr-at-eol: ignore carriage return (CR) at the end of a line when comparing.--ignore-space-at-eol: ignore changes in whitespace at the end of a line.-b,--ignore-space-change: ignore changes in the amount of whitespace. It ignores whitespace at the end of a line and treats all other sequences of one or more whitespace characters as equivalent.-w,--ignore-all-space: ignore whitespace when comparing lines. It ignores differences even if one line has whitespace where the other line has none.--ignore-blank-lines: ignore changes whose lines are all blank.
The key difference between -b and -w
These two are often confused. The crux is whether they distinguish between whitespace being there and not being there.
| Left | Right | -b (ignore amount of whitespace) | -w (ignore all whitespace) |
|---|---|---|---|
a b | a b | Same | Same |
a b | ab | Different | Same |
x = 1 | x=1 | Different | Same |
if (a) + trailing spaces | if (a) | Same | Same |
| Indented with 1 tab | Indented with 4 spaces | Same | Same |
-b collapses each run of whitespace to a single space, while -w removes all whitespace. That is why -w treats x = 1 and x=1, and even int a and inta, as the same.
The danger when indentation carries meaning
In files where indentation is part of the syntax, such as Python, YAML, and Makefiles, whitespace options can hide real changes.
if ready:
start()
notify()
if ready:
start()
notify()
In the second version, notify() has moved out of the if statement and the behavior has changed, but comparing with -w removes all whitespace, so the difference disappears. -b does catch it, because the leading whitespace went from "present" to "absent". However, if the indentation changes from 4 to 8 spaces and the code moves one block deeper, only the amount of whitespace has changed, so -b will not show it either.
The safe approach is to compare files where indentation matters with whitespace options turned off. Use whitespace options to confirm that a change is "only cleanup", and make the final check without them.
The scope of ignoring blank lines (-B)
-B and git's --ignore-blank-lines hide changes consisting only of blank lines. If an added blank line sits in the same hunk as another real change, that blank line may still be shown. This is useful for documents where the number of blank lines between paragraphs changed, or when a code formatter adjusted the blank lines between functions.
How this site's options map
| This site | Closest GNU diff | Closest git diff |
|---|---|---|
| Whitespace: Compare as is | (no option) | (no option) |
| Whitespace: Ignore trailing whitespace | -Z | --ignore-space-at-eol |
| Whitespace: Ignore amount of whitespace | -b | -b |
| Whitespace: Ignore all whitespace | -w | -w |
| Ignore case | -i | (none) |
| Ignore blank lines | Similar to -B (blank lines are excluded from comparison) | Similar to --ignore-blank-lines |
| Normalize line endings | --strip-trailing-cr + ignore the final newline | Similar to --ignore-cr-at-eol |
This site's "Ignore blank lines" excludes blank lines from the comparison entirely and only shows them in a faded color. The ignore options also apply to in-line highlighting: with "Ignore amount of whitespace" on, parts that differ only in the number of spaces are not highlighted.
Try it in this tool
Put the examples from the table above into the Text Diff Checker and switch the whitespace options one at a time to see the difference immediately. "Invisible differences" such as a CR at the end of a line or a missing final newline are covered in Line endings and invisible differences, and how to read the result as a patch in How to read a unified diff.