I know removing trailing whitespace can be done with a pre-commit hook. I am interested in doing it manually. I read the question here:
Make git automatically remove trailing whitespace before committing - Stack Overflow
The answer closest to what I want is the "automatic version" from ntc2:
(export VISUAL=: && git -c apply.whitespace=fix add -ue .) && git checkout . && git reset
That command works well except it seems to be only for changes on files that are already in the repo, not new files. I have a bunch of files that are new, meaning they aren't yet in the repo. I want to remove whitespace from those files so I tried add -A instead of -u but that didn't make a difference.
To manually clean up whitespace from your last 3 commits, you can do this:
git rebase --whitespace=fix HEAD~3
When I work on a topic branch, I track the upstream branch (usually by creating it like this)
git checkout -b topic -t
Which allows me to drop the last argument from git rebase
. So once I'm done & ready to merge, I can clean the whole topic branch quickly with:
git ws
# aliased to rebase --whitespace=fix
Note that, unlike the HEAD~3 example, this will actually rebase your changes upon the upstream branch if it's changed! (But that's also what I want, in my workflow.)