Skip to content

Instantly share code, notes, and snippets.

@KyleJamesWalker
Created March 20, 2018 19:57
Show Gist options
  • Save KyleJamesWalker/3fe41fbd46b96a226224df33a45b8ee3 to your computer and use it in GitHub Desktop.
Save KyleJamesWalker/3fe41fbd46b96a226224df33a45b8ee3 to your computer and use it in GitHub Desktop.
Protect Master Branch

How to protect master

  • Run:git config --global core.hooksPath ~/githooks
  • Create hooks folder: mkdir ~/githooks
  • Copy pre-push to the ~/githooks folder
  • Set executable: chmod +x ~/githooks/pre-push
#!/bin/bash
protected_branch='master'
current_branch=$(git symbolic-ref HEAD | sed -e 's,.*/\(.*\),\1,')
if [ $protected_branch = $current_branch ]
then
read -p "You're about to push master, is that what you intended? [y|n] " -n 1 -r < /dev/tty
echo
if echo $REPLY | grep -E '^[Yy]$' > /dev/null
then
exit 0 # push will execute
fi
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