Skip to content

Instantly share code, notes, and snippets.

@M0nica
Last active February 28, 2019 04:52
Show Gist options
  • Save M0nica/7d5813acc44391d83a46a013afa386f9 to your computer and use it in GitHub Desktop.
Save M0nica/7d5813acc44391d83a46a013afa386f9 to your computer and use it in GitHub Desktop.
A bash script that can be used in prepush Git hook to warn when pushing changes to master or an otherwise protected branch
protected_branch='master'
current_branch=$(git symbolic-ref HEAD | sed -e 's,.*/\(.*\),\1,')
# define bash colors to display
yellow='\033[0;33m'
red='\033[0;31m'
green='\033[0;32m'
no_color='\033[0m'
if [ $protected_branch = $current_branch ];
then echo "${yellow}You're about to push to $protected_branch, is that what you intended? [y|n]${no_color}"
read < /dev/tty
if echo $REPLY | grep -E '[^Yy]$' > /dev/null; then
echo "${red}This commit has been aborted.${no_color}"
exit 1 # push will not execute if there is not explicit 'Y' typed
else
echo "${green}This commit is proceeding...${no_color}"
exit 0 # push will execute
fi
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