Skip to content

Instantly share code, notes, and snippets.

@agateau
Last active April 4, 2017 09:07
Show Gist options
  • Save agateau/d17814967410d1c68ac2d59d7b5f3724 to your computer and use it in GitHub Desktop.
Save agateau/d17814967410d1c68ac2d59d7b5f3724 to your computer and use it in GitHub Desktop.
Git hook to prevent pushing to a list of branches
#!/bin/bash
# Copy this as .git/hooks/pre-push
# Customize PROTECTED_BRANCHES
# Make it executable
set -e
PROTECTED_BRANCHES="master release"
confirm() {
local local_branch
local remote_branch
local_branch=$1
remote_branch=$2
echo "Pushing to $remote_branch"
read -p "You're about to push $local_branch to $remote_branch, is that what you intended? [y|n] " -n 1 -r < /dev/tty
echo
if echo $REPLY | grep -E '^[Yy]$' > /dev/null ; then
echo "Proceeding"
else
exit 1 # push will not execute
fi
}
IFS=' '
while read local_ref local_sha remote_ref remote_sha ; do
for branch in $PROTECTED_BRANCHES ; do
if [ "$remote_ref" = "refs/heads/$branch" ] ; then
confirm $local_ref $branch
exit 0
fi
done
done
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment