Last active
June 7, 2023 23:57
-
-
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
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/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