Skip to content

Instantly share code, notes, and snippets.

@ainth
Forked from stefansundin/install-pre-push.sh
Last active September 3, 2015 15:01
Show Gist options
  • Save ainth/9cb2ae4270136aa9f7c7 to your computer and use it in GitHub Desktop.
Save ainth/9cb2ae4270136aa9f7c7 to your computer and use it in GitHub Desktop.
Git pre-push hook to prevent force pushing the master branch.
#!/bin/sh
# This script will install a Git pre-push hook that prevents force pushing the master branch.
# Install in current Git repo:
# curl -fL https://goo.gl/TgTP60 | sh
# Uninstall:
# rm .git/hooks/pre-push
# in each repository that you've added this to.
GITROOT=`git rev-parse --show-toplevel 2> /dev/null`
echo
echo
if [ "$GITROOT" == "" ]; then
echo This does not appear to be a git repo.
exit 1
fi
if [[ -f "$GITROOT/.git/hooks/pre-push" ]]; then
echo There is already a pre-push hook installed. Delete it first.
echo
echo " rm '$GITROOT/.git/hooks/pre-push'"
echo
exit 2
fi
echo Downloading pre-push hook from https://gist.githubusercontent.com/ainth/9cb2ae4270136aa9f7c7/raw/ef7a5d9990ddbd7acd4568bb07a94f21a4c44454/pre-push
echo
curl -fL -o "$GITROOT/.git/hooks/pre-push" "https://gist.githubusercontent.com/ainth/9cb2ae4270136aa9f7c7/raw/ef7a5d9990ddbd7acd4568bb07a94f21a4c44454/pre-push"
if [[ ! -f "$GITROOT/.git/hooks/pre-push" ]]; then
echo Error downloading pre-push script!
exit 3
fi
chmod +x "$GITROOT/.git/hooks/pre-push"
echo "You're all set!"
exit 0
#!/bin/bash
# Prevents force-pushing to master.
# Based on: https://gist.github.com/pixelhandler/5718585
# Install:
# cd path/to/git/repo
# curl -fL -o .git/hooks/pre-push {{urltothisfile}}
# chmod +x .git/hooks/pre-push
BRANCH=`git rev-parse --abbrev-ref HEAD`
PROTECTED_BRANCHES="^(integration|release|master)"
if [[ "$BRANCH" =~ $PROTECTED_BRANCHES ]]; then
echo "You may not push to a protected branch. You should make a pull request instead."
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