Sub topic 12
Ignoring whitespace and diffing by word
This chapter assumes you can read a unified diff and run git diff with the usual targets (working tree, index, HEAD). It covers two ways to make a noisy diff easier to read: ignoring whitespace changes and comparing at word level instead of line level.
Why line diffs get noisy
Git compares files line by line. If you re-indent a block or change a line ending, every affected line shows as one removal and one addition, even though the code did not change. A real one-word edit can also hide inside a long line that is shown twice in full. The options below reduce that noise.
Whitespace options
Git has several options that control which whitespace differences count. They work on git diff, git log -p, and git show.
| Option | What it ignores |
|---|---|
| `-w` (`--ignore-all-space`) | All whitespace when comparing lines |
| `-b` (`--ignore-space-change`) | Changes in the amount of whitespace, but not whitespace added where there was none |
| `--ignore-space-at-eol` | Whitespace at the end of a line |
| `--ignore-blank-lines` | Added or removed blank lines |
Try the strongest one first:
git diff -wSuppose a commit wraps return config in an if block and indents it. Plain git diff marks the line as removed and added. With -w, the line is no longer a change, and only the new if line remains.
Use -b when a+b and a + b are different to you, but one space and two spaces are the same. Note that -w treats a+b and a + b as equal, which can hide a real change in a language where spacing matters, such as Python indentation or a Makefile.
git diff -b HEAD~1 HEAD
git log -p -w -n 3The second command shows patches for the last three commits with whitespace ignored.
Word-level diffs
--word-diff shows changes inline within a line. Added words appear as {+word+} and removed words as [-word-].
git diff --word-diffFor a file where one line changed from timeout = 30 to timeout = 60, the output looks like this:
@@ -4,1 +4,1 @@
timeout = [-30-]{+60+}You see exactly which token changed. This is most useful for prose, documentation, and long lines.
By default, Git splits words on whitespace. Use --word-diff-regex to define what a word is, or --color-words for colored output without the markers:
git diff --word-diff-regex='[A-Za-z0-9_]+'
git diff --color-wordsWith the regex above, punctuation separates words, so foo(a, b) changing to foo(a, c) reports only b to c.
Common errors
- Trusting
-won whitespace-sensitive files. In Python, YAML, or Makefiles, a whitespace change can alter behavior. Check without-wbefore you conclude that a commit changed nothing. - Expecting
-wto change what is committed. These options only affect display. The commit still contains the whitespace edits. - Misreading word-diff markers.
[-old-]{+new+}is one replacement. A word with only{+...+}was inserted. - Combining options and getting an empty diff. If
git diff -wprints nothing butgit diffprints changes, all changes were whitespace.
Exercise
Exercise
A teammate re-indented a 200-line function and also changed one condition from x > 0 to x >= 0. Which command shows the real change most clearly, and why?
Show answer
git diff -w (or git show -w for a commit). It ignores the indentation changes, so only the line with the condition change remains. Adding --word-diff would highlight just [->-]{+>=+} within that line. Check the result without -w if the file is whitespace-sensitive.
Exercise
What is the difference between -w and -b?
Show answer
-w ignores all whitespace when comparing lines, so a+b and a + b match. -b ignores only changes in the amount of whitespace, so a+b and a + b still differ, while one space and two spaces match.