-
-
Save Hammadk/0a196dd1ac5ad971df1a to your computer and use it in GitHub Desktop.
#!/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 |
This code is dirty (I don't know how to factorise on shell...) but it takes in count all cases.
(when checkedout at master)
git push
(when checkedout at a not restricted branch)
git push origin master
(when checkedout at a not restricted branch)
git push --all
!/bin/bash
protected_branch='master'
current_branch=$(git symbolic-ref HEAD | sed -e 's,./(.),\1,')
if [ $protected_branch = $current_branch ]
then
read -p "You're about to push master, is that what you intended? [y|n] " -n 1 -r < /dev/tty
echo
if echo
then
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 [[
exit 0
fi
echo "Push aborted."
exit 1
fi
exit 0 # push will execute
fi
exit 1 # push will not execute
else
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 [[
exit 0
fi
echo "Push aborted."
exit 1
fi
exit 0 # push will execute
fi
That's a good point @sgarciafer. I almost never git push --all
so I didn't come across that issue.
The idea is great but the script is not working as explained.
The $BRANCH is the branch that has been checked out locally, not the one at origin. Also doing push --all the protected branches get pushed without confirmation message, a bit more refinement on the code would do it ;-)