Skip to content

Instantly share code, notes, and snippets.

@sergeylukin
Last active October 7, 2015 13:58
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save sergeylukin/3175467 to your computer and use it in GitHub Desktop.
Save sergeylukin/3175467 to your computer and use it in GitHub Desktop.
Git hook (pre-receive): commit any local changes before PUSH accepted
#!/bin/sh
#
# 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
#
WORKTREE=../myproject
GITDIR=$WORKTREE.git
cd $WORKTREE
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
else
echo "OK No local changes"
exit 0
fi
# return to git directory
cd $GITDIR
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment