Skip to content

Instantly share code, notes, and snippets.

@yesmeck
Created December 15, 2011 07:50
Show Gist options
  • Save yesmeck/1480264 to your computer and use it in GitHub Desktop.
Save yesmeck/1480264 to your computer and use it in GitHub Desktop.
Git pre-commit hook
#!/bin/sh
#
# A git hook script to find and fix trailing whitespace
# in your commits. Bypass it with the --no-verify option
# to git-commit
#
# Find files with trailing whitespace
for file in `git diff --check --cached | grep '^[^+-]' | grep -o '^.*[0-9]\+:'` ; do
file_name=`echo ${file} | grep -o '^[^:]\+'`
line_number=`echo ${file} | grep -oP '(?<=:)[0-9]+(?=:)'`
(sed -i "${line_number}s/\s*$//" ${file_name} > /dev/null 2>&1 \
|| sed -i '' -E "${line_number}s/\s*$//" ${file_name})
git add ${file_name}
done
# Now we can commit
exit
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment