Thoughts on development, design and the world we live in.

Git pre-commit hook to trim Xcode trailing whitespace

By paul in git, iOS. Posted on July 11th

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
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.

By paul | Posted in git, iOS | Comments (0)

Post a Comment

Your email is never shared. Required fields are marked *