Skip to content

Instantly share code, notes, and snippets.

@francistm
Created December 21, 2014 19:00
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save francistm/3e77ee6a954fd1b47ea5 to your computer and use it in GitHub Desktop.
Save francistm/3e77ee6a954fd1b47ea5 to your computer and use it in GitHub Desktop.
Git pre-commit hook
#!/bin/sh
#
# 这个挂钩会从提交的文件中删除行末的空格,并且终止 "git commit" 。
# 只需要简单的重复上一次的 "git commit" 命令重新提交一下即可。
#
# 将此文件放入 .git/hooks/pre-commit,并且用 chmod +x 添加可执行权限即可。
if git rev-parse --verify HEAD >/dev/null 2>&1
then
against=HEAD
else
# 初始化提交: 与一个空的树对象做对比
against=4b825dc642cb6eb9a060e54bf8d69288fbee4904
fi
if test "$(git diff-index --check --cached $against --)"
then
echo "COMMIT ABORTED! Removing trailing whitespaces..."
for FILE in `git diff-index --check --cached $against -- | sed '/^[+-]/d' | cut -d: -f1 | uniq`; do echo "* $FILE" ; sed -i "" 's/ *$//' "$FILE" ; done
echo "Done! Run git commit once again."
exit 1
fi
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment