Skip to content

Instantly share code, notes, and snippets.

@u-mulder u-mulder/pre-commit.sample

Last active Jan 10, 2017
Embed
What would you like to do?
Simple pre-commit hook to abort commiting if remote branch has new commits
#!/bin/bash
#
# 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
CGREEN='\033[0;32m'
NC='\033[0m'
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
fi
exit 0
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.