Git pre-commit hook to trim Xcode trailing whitespace
Xcode, even as new as v4.3 sometimes inserts trailing whitespace in edited source files. For example, when you select all lines and select menu Editor->Structure->Re-Indent to reformat the indentations. This can cause unnecessary changes to repository, when actually nothing was changed.
To solve the problem, you can use a git pre commit hook.
1. Set your git repository to highlight trailing spaces in diffs:
git config core.whitespace trailing-space
2. Add this script to run before every commit by saving it in .git/hooks/pre-commit
3. Make it executable:
chmod a+x .git/hooks/pre-commit
Notes:
The above script works even when your repository has files or folders with spaces in them.
There used to be an Xcode plugin that automatically removes them, but with Xcode 4 it requires manual invocation.