Git add everything but whitespace changes

Robin Kim
Robin Kim
Aug 13, 2015 · 1 min read

Note: This was originally posted on my blog at https://therobinkim.com/blog/git-add-everything-but-whitespace-changes. Any updates will appear there and not here.

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

After this command:
1. git diff --cached 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)

Hooray!

therobinkim

let’s learn

Robin Kim

Written by

Robin Kim

Full stack software engineer. I teach at Hack Reactor Remote @ Galvanize.

therobinkim

let’s learn

Welcome to a place where words matter. On Medium, smart voices and original ideas take center stage - with no ads in sight. Watch
Follow all the topics you care about, and we’ll deliver the best stories for you to your homepage and inbox. Explore
Get unlimited access to the best stories on Medium — and support writers while you’re at it. Just $5/month. Upgrade