Skip to content

Instantly share code, notes, and snippets.

@naoisegolden
Created May 18, 2015 16:02
Show Gist options
  • Save naoisegolden/96628dc63a2aab514880 to your computer and use it in GitHub Desktop.
Save naoisegolden/96628dc63a2aab514880 to your computer and use it in GitHub Desktop.
Git pre-push hook to prevent force pushing to branches
#!/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