I have my text editor to automatically trim trailing whitespace upon saving a file, and I am contributing to an OpenSource project that has severe problems with trailing whitespace.
Every time I try to submit a patch I must first ignore all whitespace-only changes by hand, to choose only the relevant information. Not only that, but when I run
git add -p
@Frew solution wasn't quite what I needed, so this is the alias I made for the exact same problem:
alias.addnw=!sh -c 'git diff -U0 -w --no-color "$@" | git apply --cached --ignore-whitespace --unidiff-zero -'
Or you can simply run:
git diff -U0 -w --no-color | git apply --cached --ignore-whitespace --unidiff-zero -
--unidiff-zero respectively to workaround context matching issues, according to this comment.
Basically it applies the patch which would be applied with
add without whitespace changes. You will notice that after a
git addnw your/file there will still be unstaged changes, it's the whitespaces left.
The --no-color isn't required but as I have colors set to always, I have to use it. Anyway, better safe than sorry.