Created
May 18, 2015 16:02
-
-
Save naoisegolden/96628dc63a2aab514880 to your computer and use it in GitHub Desktop.
Git pre-push hook to prevent force pushing to branches
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/bin/bash | |
# Prevents force-pushing to master | |
# Based on: https://gist.github.com/pixelhandler/5718585#comment-1390358 | |
# Install: | |
# cd path/to/git/repo | |
# curl -fL -o .git/hooks/pre-push https://gist.githubusercontent.com/naoisegolden/96628dc63a2aab514880/raw/pre-push | |
# chmod +x .git/hooks/pre-push | |
BRANCH=`git rev-parse --abbrev-ref HEAD` | |
PUSH_COMMAND=`ps -ocommand= -p $PPID` | |
PROTECTED_BRANCHES="^(master|preview|test-branch)" | |
FORCE_PUSH="force|delete|-f" | |
if [[ "$BRANCH" =~ $PROTECTED_BRANCHES && "$PUSH_COMMAND" =~ $FORCE_PUSH ]]; then | |
echo "Prevented force-push to protected branch \"$BRANCH\" by pre-push hook" | |
echo "If you really want to do this, use --no-verify to bypass this pre-push hook." | |
exit 1 | |
fi | |
exit 0 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment