Skip to content

Instantly share code, notes, and snippets.

@stepharr
Created May 5, 2018 21:16
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save stepharr/e7cb34b2b7846c8f571740d78e2bde0e to your computer and use it in GitHub Desktop.
Save stepharr/e7cb34b2b7846c8f571740d78e2bde0e to your computer and use it in GitHub Desktop.
A git hook to prevent push's if not based on latest in master
#!/bin/bash
red='\033[0;31m'
green='\033[0;32m'
yellow='\033[0;33m'
no_color='\033[0m'
BRANCH="$(git symbolic-ref HEAD 2>/dev/null)"
if [ $? != 0 ]
then
DETATCHED="detached HEAD at $(git rev-parse --short HEAD)"
echo -e "\n${red} You are currently on ${DETATCHED}. Please make sure you're on a valid branch before pushing. ${no_color}\n"
exit 1
else
BRANCH=${BRANCH##refs/heads/}
if git merge-base --is-ancestor refs/remotes/origin/master ${BRANCH};
then
echo -e "\n${green}Pushing \`${BRANCH}\` to remote.${no_color}\n"
else
# Write to standard error
echo -e "\n${red}Error branch \`${BRANCH}\` is not based off the latest commits in master. Please merge in changes from master or rebase branch \`${BRANCH}\` off the changes in master, and then try pushing again.${no_color}\n"
exit 1
fi
fi
exit 0
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment