@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 -
Update
Added options -U0
, and --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.
Warning
While this trick works as-is, if you try to use it to drop blank line changes with --ignore-blank-lines
then things get complicated. With this option, git diff
will just drop some chunks, making the resulting patch bogus since the line numbers in the destination file are going to be off.