Skip to content

Instantly share code, notes, and snippets.

@jonatasnona
Created July 7, 2012 15:03
Show Gist options
  • Save jonatasnona/3066806 to your computer and use it in GitHub Desktop.
Save jonatasnona/3066806 to your computer and use it in GitHub Desktop.
Git: pre-commit
#!/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
@jonatasnona
Copy link
Author

name this script as pre-commit and place it in the .git/hooks folder of your git repository, then apply chmod +x in .git/hooks/pre-commit.

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