Skip to content

Instantly share code, notes, and snippets.

@chales
Created June 23, 2015 22:23
Show Gist options
  • Save chales/13306eb261da13b5facd to your computer and use it in GitHub Desktop.
Save chales/13306eb261da13b5facd to your computer and use it in GitHub Desktop.
Git pre-push hook used to prevent force pushes
#!/bin/bash
# Prevents force-pushing to proceted branches
CURRENT_BRANCH=`git rev-parse --abbrev-ref HEAD`
PROTECTED_BRANCHES="^(develop|master|release*)"
PUSH_COMMAND=`ps -ocommand= -p $PPID`
FORCE_OPTION="delete|force|-f"
if [[ "$CURRENT_BRANCH" =~ $PROTECTED_BRANCHES && "$PUSH_COMMAND" =~ $FORCE_OPTION ]]; then
echo "Prevented force-push to protected branch '$CURRENT_BRANCH'!"
echo "You should only Force Push to protected branches if you fully understand the effect."
exit 1
fi
exit 0
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment