A code formatter, a changed editor setting or a merge from a branch with different line endings can touch every line of a file. The diff then shows hundreds of changes where there is one real edit. Ignoring whitespace is the usual cure, but it is only safe when whitespace carries no meaning.
Where ignoring whitespace is usually safe
In JavaScript and PHP, indentation and extra spaces are cosmetic. Switch on Ignore whitespace to review a reformatted file and see only the changes in the code itself.
Watch out for whitespace that is data: inside string literals, template literals and heredocs, spaces are part of the value.
Where it is not
Python's blocks are defined by indentation. These two functions are different programs:
def total(items):
result = 0
for item in items:
result += item
return result
def total(items):
result = 0
for item in items:
result += item
return result
For Python (and YAML), the comparison therefore ignores only trailing spaces and keeps indentation visible.
A practical routine
- Compare once with whitespace visible to see how large the formatting change is.
- Switch on ignore whitespace to find the real edits.
- For indentation-sensitive languages, never rely on a whitespace-blind diff for the final review.
Questions
Is it safe to ignore whitespace in JavaScript?
Usually, yes. Whitespace outside of strings rarely changes behaviour, so it is a good way to review a reformatted file. Be careful with whitespace inside template literals and strings, which does matter.
Why is Python different?
Python uses indentation to define blocks. Moving a line one level in or out can change which block it belongs to, so only trailing spaces are safe to ignore.