Skip to content

Instantly share code, notes, and snippets.

@bdurand
Last active January 9, 2020 21:31
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 bdurand/f66ebc461c2f8debfc394ef93a97c927 to your computer and use it in GitHub Desktop.
Save bdurand/f66ebc461c2f8debfc394ef93a97c927 to your computer and use it in GitHub Desktop.
Git pre-push hook to prevent you from accidentally pushing to important branches.
#!/bin/bash
# This script can be used as a git pre-push hook to guard against accidentally
# pushing to remote branches that may have special meaning and trigger other
# behavior. If you try to push to one of these branches, you will be prompted
# to confirm that is what you really meant to do.
set -o errexit
# set -o xtrace
# List of protected branches that you must confirm before pushing to.
protected_branches='master release deploy sprint staging'
current_branch=$(git symbolic-ref HEAD | sed -e 's,.*/\(.*\),\1,')
if [[ " ${protected_branches} " = *" ${current_branch} "* ]]
then
echo -en "\033[31mYou're about to commit or push to ${current_branch}, is that what you intended? [y|N] \033[0m"
echo -en "\033[1m"
read -r < /dev/tty
echo -en "\033[0m"
echo
if echo $REPLY | grep -E '^[Yy]$' > /dev/null
then
exit 0 # push will execute
fi
echo -e "\033[31m\033[1mPush aborted\033[0m"
exit 1 # push will not execute
else
exit 0 # push will execute
fi
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment