Skip to content

Instantly share code, notes, and snippets.

@repsejnworb
Last active February 25, 2020 13:04
Show Gist options
  • Select an option

  • Save repsejnworb/e1dc8786c8b314f853cb487569a18aba to your computer and use it in GitHub Desktop.

Select an option

Save repsejnworb/e1dc8786c8b314f853cb487569a18aba to your computer and use it in GitHub Desktop.
Prevent push to master
#!/bin/sh
# This script will install a Git pre-push hook that prevents pushing the master branch.
# Based on https://gist.github.com/stefansundin/d465f1e331fc5c632088
# Global installation instructions:
# mkdir $HOME/.githooks
# git config --global core.hooksPath $HOME/.githooks
# curl -fL -o $HOME/.githooks/pre-push https://gist.githubusercontent.com/repsejnworb/e1dc8786c8b314f853cb487569a18aba/raw/pre-push
# chmod +x $HOME/.githooks/pre-push
# Uninstall:
# rm $HOME/.githooks/pre-push
# Single repo installation:
# curl -fL https://gist.githubusercontent.com/repsejnworb/e1dc8786c8b314f853cb487569a18aba/raw/install-pre-hook.sh | sh
# Uninstall:
# rm .git/hooks/pre-push
GIT_DIR=`git rev-parse --git-common-dir 2> /dev/null`
echo
echo
if [ "$GIT_DIR" == "" ]; then
echo This does not appear to be a git repo.
exit 1
fi
if [ -f "$GIT_DIR/hooks/pre-push" ]; then
echo There is already a pre-push hook installed. Delete it first.
echo
echo " rm '$GIT_DIR/hooks/pre-push'"
echo
exit 2
fi
echo Downloading pre-push hook from https://gist.githubusercontent.com/repsejnworb/e1dc8786c8b314f853cb487569a18aba/raw/install-pre-hook.sh
echo
curl -fL -o "$GIT_DIR/hooks/pre-push" "https://gist.githubusercontent.com/repsejnworb/e1dc8786c8b314f853cb487569a18aba/raw/pre-push"
if [ ! -f "$GIT_DIR/hooks/pre-push" ]; then
echo Error downloading pre-push script!
exit 3
fi
chmod +x "$GIT_DIR/hooks/pre-push"
echo "You're all set! Happy hacking!"
echo "P.S. There is now a way to install this globally, see the instructions on the gist page."
exit 0
#!/bin/bash
# Prevents pushing to master.
# Based on: https://gist.github.com/stefansundin/d465f1e331fc5c632088
# Install:
# cd path/to/git/repo
# curl -fL -o .git/hooks/pre-push https://gist.githubusercontw/pre-push
# chmod +x .git/hooks/pre-push
BRANCH=$(git rev-parse --abbrev-ref HEAD)
PUSH_COMMAND=$(ps -ocommand= -p $PPID)
if [[ "$BRANCH" == "master" ]]; then
echo
echo "Prevented force-push to $BRANCH. Don't be a noob."
echo "If you really want to do this, use --no-verify to bypass this pre-push hook."
echo
exit 1
fi
exit 0
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment