Navigation Menu

Skip to content

Instantly share code, notes, and snippets.

@u-mulder
Last active June 7, 2023 23:57
Show Gist options
  • Star 3 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save u-mulder/b008bc41a3a18c754e0d75fe6839fab8 to your computer and use it in GitHub Desktop.
Save u-mulder/b008bc41a3a18c754e0d75fe6839fab8 to your computer and use it in GitHub Desktop.
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