I'd like to commit all my whitespace corrections in their own commit, to keep everything else pure from whitespace changes.
It's easy to filter out whitespace differences with git diff
using something like this
git diff --ignore-all-space --ignore-space-change --ignore-space-at-eol --ignore-blank-lines
but how do I get a listing of only whitespace differences?
(It would also be useful to get a list of files that only have whitespace differences, so I can just add
them all without going through them with git add -p
to pick out the whitespace differences. But I suppose that's secondary.)
Here's one way you could do it:
Start with a clean working directory (e.g. git reset --hard
)
Use Whitespace Total Fixer to clean up all the whitespace errors. There are options to fix or ignore various issues but the default may be fine:
find . -name "*.whatever_extension_you_need" -exec wtf -v -i {} \;
git diff
will now show only the whitespace changes
Stage and commit the whitespace-only changes.
(FWIW, I wrote wtf
; I'm sorry if it seems like shameless self-promotion, but I did write it specifically for situations like this one where a repository repeatedly becomes contaminated with whitespace issues that gum up your commits.)
You can also use wtf
to simply check for whitespace errors rather than in-place fixing of them; this won't affect your files but it will print a (hopefully helpful) message to stderr about the issues it has found.
> find . -name "*.whatever_extension_you_need" -exec wtf -v {} \; > /dev/null
nightmare.txt LINE 8: WARNING: spaces followed by tabs in whitespace at beginning of line
nightmare.txt:
CHOPPED 1 lines with trailing space
CHOPPED 0 blank lines at EOF
ADDED newline at EOF
CHANGED 1 line endings which didn't match crlf from first line
WARNED ABOUT 1 lines with tabs/spaces mix
You can temporarily apply the non-whitespace differences to the original, or a copy. Then you can compare using diff or git diff before reverting.
In a repository, you can do something analogous to the suggestions in Add only non-whitespace changes depending exactly what you're diffing. Something like:
git diff -U0 -w -b --ignore-blank-lines --no-color | git apply --ignore-whitespace --unidiff-zero -
git diff
When I wanted to do this with non-git files and process substitutions I used a small script that I wrote to do the same thing with temporary files.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With