Skip to content

Instantly share code, notes, and snippets.

@ePirat
Last active May 29, 2018 16:33
Show Gist options
  • Save ePirat/a6ff0d62c0ea0dad8b35aaff24549ac8 to your computer and use it in GitHub Desktop.
Save ePirat/a6ff0d62c0ea0dad8b35aaff24549ac8 to your computer and use it in GitHub Desktop.
#!/bin/bash
# Git hook to ask before pushing to specific branch or remote
# Adapted from https://blog.ghost.org/prevent-master-push/
# Put this in .git/hooks/pre-push and do not forget to make it executable
protected_remote='origin'
protected_branch='master'
current_remote="$1"
current_branch=$(git symbolic-ref HEAD | sed -e 's,.*/\(.*\),\1,')
if [ $protected_remote = $current_remote ]
then
read -p "You're about to push to $current_remote, 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
if [ $protected_branch = $current_branch ]
then
read -p "You're about to push $current_branch, 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