Skip to content

Instantly share code, notes, and snippets.

Last active October 7, 2015 13:58
What would you like to do?
Git hook (pre-receive): commit any local changes before PUSH accepted
# This hooks is placed in a Bare repository
# It makes sure that working tree doesn't contain any local changes
# And if it contains - submits a commit and returns false
# So if false returned - client should PULL and then PUSH again
# Assuming following file structure:
# .
# |-- myproject
# |-- myproject.git
# set WORKTREE=../myproject
# Where myproject.git - current bare repo and myproject - working directory
# To enable this hook, rename this file to "pre-receive" and make sure it is executable
if [ `git --work-tree=./ --git-dir=$GITDIR status | grep -c "working directory clean"` -ne 1 ]; then
echo "Changes detected. Issuing commit.."
git --work-tree=./ --git-dir=$GITDIR add . -A
git --work-tree=./ --git-dir=$GITDIR commit -m'Local changes commit'
exit 1
echo "OK No local changes"
exit 0
# return to git directory
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment