Add only non-whitespace changes

I have my text editor to automatically trim trailing whitespace upon saving a file, and I am contributing to an open source 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 rebase I usually run into several problems because of them.

As such I would like to be able to add to index only non-whitespace changes, in a way similar that git add -p does, but without having to pick all the changes myself.

Does anyone know how to do this?

EDIT: I cannot change the way the project works, and they have decided, after discussing it on the mailing list, to ignore this.


@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.


Create a patch file containing only the real changes (excluding lines with only whitespace changes), then clean your workspace and apply that patch file:

git diff > backup
git diff -w > changes
git reset --hard
patch < changes

Review the remaining differences, then add and commit as normal.

The equivalent for Mercurial is to do this:

hg diff > backup
hg diff -w > changes
hg revert --all
hg import --no-commit changes


This works for me:

If you want to keep a stash around, this works

git stash && git stash apply && git diff -w > foo.patch && git checkout . && git apply foo.patch && rm foo.patch

I don't like the stashes, but I have run into a bug in git + cygwin where I lose changes, so to make sure that stuff went to the reflog at least I set up the following:

git add . && git commit -am 'tmp' && git reset HEAD^ && git diff -w > foo.patch && git checkout . && git apply foo.patch && rm foo.patch

Basically we create a diff that doesn't include the space changes, revert all of our changes, and then apply the diff.


Top-voted answer does not work in all cases, due to whitespace in the patch context according to users in the comments.

I revised the command as follows:

$ git diff -U0 -w --no-color | git apply --cached --ignore-whitespace --unidiff-zero

This generates a patch with no context. Shouldn't be a problem since the patch is short-lived.

Corresponding alias, again a revision of what was already provided by other users:

addw = !sh -c 'git diff -U0 -w --no-color "$@" | git apply --cached --ignore-whitespace --unidiff-zero' -

Add the following to your .gitconfig:

anw = !git diff -U0 -w --no-color -- \"$@\" | git apply --cached --ignore-whitespace --unidiff-zero "#"

Thanks to @Colin Herbert's answer for the inspiration.

Syntax Explanation

The final # must be quoted so it's not treated it as a comment inside the .gitconfig, but instead gets passed through and is treated as a comment inside the shell - it is inserted between the end of the git apply and the user-supplied arguments that git automatically places at the end of the command line. These arguments aren't wanted here - we don't want git apply to consume them, hence the preceding comment character. You may want to run this command as GIT_TRACE=1 git anw to see this in action.

The -- signals end of arguments and allows for the case that you have a file named -w or something that would look like a switch to git diff.

Escaped double-quotes around $@ are required to preserve any user-supplied quoted arguments. If the " character is not escaped, it will be consumed by the .gitconfig parser and not reach the shell.

Note: .gitconfig alias parsing doesn't recognise single-quotes as anything special - its only special characters are ", \, \n, and ; (outside of a "-quoted string). This is why a " must always be escaped, even if it looks like it's inside a single-quoted string (which git is completely agnostic about).

This is important, eg. if you have a handy alias to execute a bash command in the working tree's root. The incorrect formulation is:

sh = !bash -c '"$@"' -

While the correct one is:

sh = !bash -c '\"$@\"' -