Created
May 5, 2021 16:54
-
-
Save simbathesailor/633bf0e6500cb22b80f4867c488c25d3 to your computer and use it in GitHub Desktop.
Git hook to check for master change before allow to push
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/sh | |
# https://stackoverflow.com/questions/31992627/git-pre-commit-hook-error-color | |
# COLOR CODES | |
red='\033[0;41m' | |
white='\033[0;47m' | |
green='\033[0;32m' | |
yellow='\033[0;33m' | |
no_color='\033[0m' | |
# Fetch the latest refs for origin master | |
git fetch origin master | |
# Initialize the variables | |
UPSTREAM=origin/master | |
LOCAL=$(git rev-parse @) | |
REMOTE=$(git rev-parse "$UPSTREAM") | |
BASE=$(git merge-base @ "$UPSTREAM") | |
if [ $LOCAL == $REMOTE ]; then | |
echo "${green} Great, Your branch is in sync with origin/master ${no_color}" | |
exit 0 | |
elif [ $LOCAL == $BASE ]; then | |
echo "${red} You need to take the pull from the master, as master head has some new changes ${no_color}" | |
exit 1 | |
elif [ $REMOTE == $BASE ]; then | |
echo "${green} Great, Your branch is in sync with master ${no_color}" | |
exit 0 | |
else | |
echo "${red} Sorry your branch has diverged, take pull from origin/master to proceed the push ${no_color}" | |
exit 1 | |
fi |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment