Skip to content

Instantly share code, notes, and snippets.

Last active Jan 10, 2017
What would you like to do?
Simple pre-commit hook to abort commiting if remote branch has new commits
# Checking statuses of local and remote versions of current branch
# If commits from these branches differ, then someone forgot to run `git pull`
echo "Checking for changes before commit:"
echo "Fetching new info..."
git fetch
# Get the name of a branch
BRANCH=`git rev-parse --abbrev-ref HEAD`
# Get count of different commits between branches
COMCOUNT=$(git rev-list HEAD..origin/"$BRANCH" --count)
# Add some colors
if [[ "$COMCOUNT" != "0" && "$COMCOUNT" != "" ]]; then
printf "You need to pull new changes (${CGREEN}git pull origin $BRANCH${NC}) before commiting to branch ${CGREEN}$BRANCH${NC}\n"
exit 1
exit 0
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment