This site runs best with JavaScript enabled.

git add Everything but Whitespace Changes

Robin Kim

August 13, 2015

I was fixing some spelling errors in an open source repo in GitHub, but for some reason, when I went to git diff to see which changes would be staged, I started seeing hundreds of whitespace edits I could've sworn I didn't intend to make. I tried to use git add -p to tell git which whitespace edits to ignore, but it just wasn't feasible to parse through so many false hits!

So a quick Gogle search led to this Stack Overflow answer which suggests:

git diff -w | git apply --cached --ignore-whitespace

Here, we're gathering the changes of git diff -w (which ignores whitespace changes) and pipes that output to git apply --cached --ignore-whitespace. Here's a link to that pulls in all of the necessary documentation.

After this command:

  1. git diff --staged shows the changes that have been be staged for commit (which is the stuff you want)
  2. git diff shows all the whitespace changes that wasn't staged for commit (which is exactly what you wanna toss away)


Share article