Add only non-whitespace changes

Viewed 107975

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.

12 Answers

It works for me :

git config apply.whitespace fix

Before each commit use command :

git add -up .

This is my hack.

git diff -w | grep "diff --git a/*" | sed -r 's#diff --git a/(.*) b(.*)#\1#g' | xargs git add 

git diff -w only shows files with non whitespace changes,

I have my text editor to automatically trim trailing whitespace upon saving a file

Doesn't your editor have proj / dir specific settings? Could just disable your whitespace preferences for this project. Seems like a much easier solve...

Similar to @void.pointer's answer, but for fixing the most recent commit.

git reset --mixed head^
git diff -U0 -w --no-color | git apply --cached --ignore-whitespace --unidiff-zero -

This leaves the whitespace changes unstaged and the rest staged.

Related