Skip to content

Instantly share code, notes, and snippets.

@Hammadk
Last active April 24, 2016 02:20
Show Gist options
  • Save Hammadk/0a196dd1ac5ad971df1a to your computer and use it in GitHub Desktop.
Save Hammadk/0a196dd1ac5ad971df1a to your computer and use it in GitHub Desktop.
git pre-push hook: get warned before pushing to git master branch
#!/bin/bash
# Warn before pushing to protected branches
# Make script executable with chmod +x pre-push
# Bypass with git push --no-verify
BRANCH=`git rev-parse --abbrev-ref HEAD`
PROTECTED_BRANCHES="^(master|dev|release-*|patch-*)"
if [[ "$BRANCH" =~ $PROTECTED_BRANCHES ]]; then
read -p "Are you sure you want to push to \"$BRANCH\" ? (y/n): " -n 1 -r < /dev/tty
echo
if [[ $REPLY =~ ^[Yy]$ ]]; then
exit 0
fi
echo "Push aborted."
exit 1
fi
exit 0
@Hammadk
Copy link
Author

Hammadk commented Apr 24, 2016

That's a good point @sgarciafer. I almost never git push --all so I didn't come across that issue.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment