Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I remove trailing whitespace only on changed lines in a pre-commit git hook?

Tags:

git

I've seen a number of solutions suggested, but nearly all of them strip whitespace from the entire file if it's been changed, which is not an option. I have also tried:

git diff -w --no-color | git apply --cached

but it doesn't seem to work either. I am using git version 1.6.3.1 and cannot update it.

like image 578
silvamerica Avatar asked Mar 21 '12 20:03

silvamerica


1 Answers

Found the answer:

git diff --cached --no-color > stage.diff && git apply --index -R stage.diff && git apply --index --whitespace=fix stage.diff && rm -f stage.diff
like image 178
silvamerica Avatar answered Sep 18 '22 02:09

silvamerica