Skip to content

Instantly share code, notes, and snippets.

@egel
Last active July 6, 2016 16:49
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 egel/2058f19cf78df84ade741b7a77a38006 to your computer and use it in GitHub Desktop.
Save egel/2058f19cf78df84ade741b7a77a38006 to your computer and use it in GitHub Desktop.
Git pre-push guardian to prevent push to protected branches.
#!/bin/bash
# General colors
black='\x1B[0;30m'
red='\x1B[0;31m'
green='\x1B[0;32m' # '\e[1;32m' is too bright for white bg.
blue='\x1B[1;34m'
yellow='\x1B[0;33m'
purple='\x1B[1;35m'
cyan='\x1B[0;36m'
endColor='\x1B[0m'
protected_branches=( master stage develop )
current_branch=$(git symbolic-ref HEAD | sed -e 's,.*/\(.*\),\1,')
for i in "${protected_branches[@]}"
do
if [ ${i} == ${current_branch} ]; then
echo -en "${yellow}[Git pre-push]${endColor} You're about to push to ${red}${i}${endColor}, is that what you intended?"
read -p " [y/N] " -r < /dev/tty
if echo ${REPLY} | egrep -E '^[yY]$' > /dev/null
then
echo -e "Continue of pushing to ${red}${i}${endColor}"
exit 0 # push will execute
fi
echo -en "Abort pushing to ${red}${i}${endColor}\n"
exit 1 # push will not execute
fi
done
exit 0 # push will execute when won't match protected_branches
@egel
Copy link
Author

egel commented May 9, 2016

Usage for git < 1.8.2:

cd  ~/my_repo_dir/.git/hooks 
wget https://gist.githubusercontent.com/egel/2058f19cf78df84ade741b7a77a38006/raw/bc22e8f1d877f4308c2ccdd336a82f8214d0f9f8/pre-push -O pre-push && chmod a+x pre-push

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment