Create a gist now

Instantly share code, notes, and snippets.

@tilap /pre-push
Last active Aug 29, 2015

What would you like to do?
Git Pre-push hook to avoid pushing on some branch or ask confirmation for some other branches
#!/bin/bash
## CONFIG
# Pattern of branch name you won't be allowed to push force
NOPUSHFORCE_BRANCHES_PATTERN="^(dev|release-*|patch-*)"
# Pattern of branch name you won't be allowed to push
NOPUSH_BRANCHES_PATTERN="^(master)"
# Pattern of branch name that will ask to confirm before pushing
CONFIRM_BRANCH_PATTERN="^(master|production)"
## VARS
FORCE_PUSH_PATTERN="force|delete|-f"
CURRENT_BRANCH=$(git symbolic-ref HEAD | sed -e 's,.*/\(.*\),\1,')
PUSH_COMMAND=`ps -ocommand= -p $PPID`
## Deny push force
if [[ "$CURRENT_BRANCH" =~ $NOPUSHFORCE_BRANCHES_PATTERN && "$PUSH_COMMAND" =~ $FORCE_PUSH_PATTERN ]]; then
echo "Pre-push hook: push force on branch \"$CURRENT_BRANCH\" is not allowed"
exit 1
fi
## Deny simple push
if [[ "$CURRENT_BRANCH" =~ $NOPUSH_BRANCHES_PATTERN ]]; then
echo "Pre-push hook: push on branch \"$CURRENT_BRANCH\" is not allowed"
exit 1
fi
## Ask confirmation
if [[ "$CURRENT_BRANCH" =~ $CONFIRM_BRANCH_PATTERN ]]
then
read -p "You're about to push on \"$CURRENT_BRANCH\". Are you really sure? [Y|n] " -n 1 -r < /dev/tty
echo
if echo $REPLY | grep -E '^[YyOo]$' > /dev/null ; then
exit 0
else
exit 1
fi
fi
exit 0
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment