Git diff -w ignore whitespace only at start end of lines
Interview Questions practice on Codemia
Over 8,000 real interview questions from top companies, searchable by company and role.
Introduction
git diff -w is often suggested as a quick way to hide whitespace noise, but it is much broader than many people want. It ignores whitespace changes everywhere in the line, not just at the start or end. If your goal is to ignore only leading and trailing whitespace while still seeing meaningful spacing changes inside the line, Git does not have a single built-in flag for that exact rule.
What Git Can Ignore Natively
Git already supports several whitespace-related diff options, but each one solves a slightly different problem.
Here is what they mean in practice:
- '
-wor--ignore-all-spaceignores all whitespace changes' - '
-bor--ignore-space-changeignores changes in the amount of whitespace' - '
--ignore-space-at-eolignores trailing whitespace only'
Notice the gap. Git can ignore end-of-line spaces, and it can ignore all spaces, but it cannot natively say "ignore both leading and trailing spaces, but keep internal spacing visible."
Why -w Is Usually Too Broad
Consider these two lines:
With git diff -w, Git treats them as the same because all whitespace differences are suppressed. That may be acceptable in some reviews, but it is not the same as ignoring only the padding at the edges of the line.
Leading whitespace can also matter in real code review, especially in indentation-sensitive languages or in files where alignment carries meaning for humans. That is why -w can hide more than you intended.
The Closest Safe Built-In Option
If the problem is mostly trailing spaces from editors or formatters, use the narrow built-in option:
That filters out whitespace added or removed at the end of each line and keeps leading indentation and internal spacing changes visible. For many teams, that is the right compromise because trailing-space churn is the common nuisance.
How To Ignore Leading And Trailing Whitespace Exactly
If you really need the precise rule, the usual approach is to compare normalized content instead of asking Git to do a mode it does not support.
One simple pattern is to trim both sides before diffing them:
That strips whitespace at the start and end of each line while leaving spaces inside the line unchanged.
You can also package the rule in a reusable helper script:
This is not a native git diff flag, but it is exact and easy to explain.
Team-Wide Alternatives
If the same normalization rule matters across a whole codebase, you can look at .gitattributes, textconv, or a custom external diff driver. That gives Git a consistent way to preprocess files before comparison.
Those solutions are powerful, but they are also heavier to maintain. For ordinary source review, a small helper script or one-off normalized diff is usually simpler and clearer.
Common Pitfalls
The most common mistake is assuming -w means "ignore only whitespace at the edges of the line." It does not.
Another issue is using -w and then missing spacing changes that reviewers actually care about, such as altered indentation or changed formatting inside expressions.
Developers also sometimes overlook --ignore-space-at-eol, which is often enough if the noise is only trailing spaces.
Finally, avoid building a complicated Git configuration when the requirement is occasional and local. A normalization script is often easier to understand and easier to remove later.
Summary
- '
git diff -wignores all whitespace, not only leading and trailing spaces.' - '
--ignore-space-at-eolhandles trailing spaces only.' - Git has no single native flag for "ignore leading and trailing whitespace only."
- For that exact behavior, compare normalized content with an external script or diff step.
- Choose the narrowest whitespace-ignore rule that matches the review problem.
Related reading
- Git diff between current branch and master but not including unmerged master commits
- git diff between two different files
- Git diff output to file preserve coloring
- Git diff says subproject is dirty
- Git Difference between HEAD, working tree and index?
- git empty ident name for not allowed
- Git error - gpg failed to sign data
- git error failed to push some refs to remote
.png&w=3840&q=75)
Tackling System Design Interview Problems
A short course that equips you with the skills to approach system design interviews methodically.
Start the free courseTrack what you have practised
A free account saves your progress, solutions and study plan across every problem on Codemia.
Interview Questions practice on Codemia
Over 8,000 real interview questions from top companies, searchable by company and role.