Use git pre-commit hook to check unwanted trailing whitespace

Trailing whitespaces are always boring. So when we use git, we can set a pre-commit hook to check this for us.

Put following shell to your .git/hook folder.

#!/bin/sh

red="\033[1;31m"
color_end="\033[0m"

# Check unwanted trailing whitespace or space/tab indents;

if [[ `git diff --cached --check` ]]; then
    echo -e ${red}Commit failed${color_end}
    git diff --cached --check
    exit 1
fi

Don’t forget give execute permission to it.

$ chmod +x pre-commit
Meck 15 December 2011
blog comments powered by Disqus