Skip to content

Instantly share code, notes, and snippets.

@holysugar
Created October 27, 2011 03:15
Show Gist options
  • Star 5 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save holysugar/1318698 to your computer and use it in GitHub Desktop.
Save holysugar/1318698 to your computer and use it in GitHub Desktop.
git pre-commit warning to trailing whitespaces
#!/bin/sh
function error() {
echo "'$1' has trailing spaces.\n" >&2
}
git diff --cached --name-only | (while read f; do
ERROR=0
if grep -n '[[:space:]]$' "$f" ; then
error $f
ERROR=1
fi
done; exit $ERROR)
if [ $? -eq 0 -o "$IGNORE_WHITESPACE" ]; then
exit
else
echo "if you know what you are doing, use 'IGNORE_WHITESPACE=1 git commit'"
exit 1
fi
@skierpage
Copy link

You need to put this in the project's .git/hooks directory, make sure it's executable using chmod a+x .git/hooks/pre-commit

/bin/sh on Ubuntu is the dash shell and the function error() definition doesn't work; one fix is to change the first line to /bin/bash.

To automatically add this to projects, read about TEMPLATE DIRECTORY in git-init help.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment