Skip to content

Instantly share code, notes, and snippets.

@area73
Last active October 9, 2020 05:33
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save area73/9783f2616c82dc451fd070661d3008d2 to your computer and use it in GitHub Desktop.
Save area73/9783f2616c82dc451fd070661d3008d2 to your computer and use it in GitHub Desktop.
Prevent git push to master git prehook
#!/bin/bash
# Prevents force-pushing to master.
# 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/area73/9783f2616c82dc451fd070661d3008d2/raw/ac5feeed9f4766195e2ec6cd6d85d3a3863cc128/pre-push
# chmod +x $HOME/.githooks/pre-push
# Uninstall:
# rm $HOME/.githooks/pre-push
BRANCH=`git rev-parse --abbrev-ref HEAD`
PUSH_COMMAND=`ps -ocommand= -p $PPID`
if [[ "$BRANCH" == "master" && "$PUSH_COMMAND" =~ push|force|delete|-f ]]; then
echo
echo "Prevented force-push to $BRANCH. This is a very dangerous command."
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