Skip to content

Instantly share code, notes, and snippets.

@alexmarles
Last active August 29, 2015 14:16
Show Gist options
  • Save alexmarles/2cba53427ec43c49aead to your computer and use it in GitHub Desktop.
Save alexmarles/2cba53427ec43c49aead to your computer and use it in GitHub Desktop.
Prevent git push --force for master branch (by @ErisDS from http://dev.ghost.org/prevent-master-push/)
#!/bin/bash
# Prevents force-pushing to master.
protected_branch='master'
current_branch=$(git symbolic-ref HEAD | sed -e 's,.*/\(.*\),\1,')
push_command=`ps -ocommand= -p $PPID`
if [[ $protected_branch = $current_branch && $push_command =~ force|delete|-f ]]
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