How to ignore whitespace in a diff
Reformatting a file can make every line look changed. Here's how to hide whitespace-only changes in Git, on the command line and in Diffspot, and when you shouldn't.
Kinds of whitespace changes
- Trailing whitespace: spaces or tabs at the end of a line, often removed by editors on save.
- Amount of whitespace: re-indentation, tabs converted to spaces, or two spaces collapsed into one.
- All whitespace: spaces added or removed anywhere, for example
a+b→a + b. - Blank lines added or removed between blocks.
- Line endings: Windows (CRLF) versus Unix (LF).
In Git
| Option | Ignores |
|---|---|
git diff --ignore-space-at-eol | Whitespace changes at line ends |
git diff -b (--ignore-space-change) | Changes in the amount of whitespace |
git diff -w (--ignore-all-space) | All whitespace when comparing lines |
git diff --ignore-blank-lines | Added or removed blank lines |
git diff --ignore-cr-at-eol | Carriage returns at line ends (CRLF vs LF) |
The same flags work with git show and git log -p. On GitHub, add ?w=1 to a pull request's "Files changed" URL to hide whitespace changes.
With the diff command
GNU diff has matching options: -Z (trailing), -b (amount), -w (all), -B (blank lines) and --strip-trailing-cr. For example, diff -u -w old.py new.py.
In Diffspot
Open the Ignore menu and choose one of Ignore trailing whitespace, Ignore changes in whitespace or Ignore all whitespace, and optionally Ignore blank lines. Line endings never count as changes. The text boxes keep the original text, so merging and exporting still use your exact content.
Ignore rules also go beyond whitespace: you can ignore letter case, mask out timestamps, dates, UUIDs, hex hashes, IP addresses and numbers, or ignore whole lines that match a regular expression, such as comment lines.
When not to ignore whitespace
In Python, YAML, Makefiles and Haskell, indentation is part of the meaning. A line moving in or out of a block is a real change that -w would hide. Review those files with whitespace visible, or at least check the indentation of the lines that remain changed.
Compare code, ignoring whitespace