Skip to content

Instantly share code, notes, and snippets.

@matthias-chlechowitz
Created April 21, 2015 11:04
Show Gist options
  • Save matthias-chlechowitz/855ea370e2365bcd6cbf to your computer and use it in GitHub Desktop.
Save matthias-chlechowitz/855ea370e2365bcd6cbf to your computer and use it in GitHub Desktop.
#!/bin/bash
# Called by "git push" after it has checked the remote status,
# but before anything has been pushed.
#
# If this script exits with a non-zero status nothing will be pushed.
#
# Steps to install, from the root directory of your repo...
# 1. Copy the file into your repo at `.git/hooks/pre-push`
# Or execute `ln -sf ./bin/git/pre-push .git/hooks/pre-push`
# 2. Set executable permissions, run `chmod +x .git/hooks/pre-push`
#
# Try a force push to master, you should get a message `*** [Policy] never force push...`
#
# The commands below will not be allowed...
# `git push --force origin master`
# `git push --delete origin master`
# `git push origin :master`
#
# Nor will a force push while on the master branch be allowed...
# `git co master`
# `git push --force origin`
#
# Requires git 1.8.2 or newer
#
# Git 1.8.2 release notes cover the new pre-push hook:
# <https://github.com/git/git/blob/master/Documentation/RelNotes/1.8.2.txt>
#
# See Sample pre-push script:
# <https://github.com/git/git/blob/87c86dd14abe8db7d00b0df5661ef8cf147a72a3/templates/hooks--pre-push.sample>
#
# Taken from https://gist.github.com/pixelhandler/5718585#comment-1390358
BRANCH=`git rev-parse --abbrev-ref HEAD`
PUSH_COMMAND=`ps -ocommand= -p $PPID`
PROTECTED_BRANCHES="^(master|candidates/release*)"
FORCE_PUSH="force|delete|-f|-uf"
if [[ "$BRANCH" =~ $PROTECTED_BRANCHES && "$PUSH_COMMAND" =~ $FORCE_PUSH ]]; then
echo "[Policy] Never force push or delete the \"$BRANCH\" branch! Prevent by pre-push hook."
exit 1
fi
exit 0
pre-push (END)
@matthias-chlechowitz
Copy link
Author

save as .git/hooks/pre-push and make it executable ;)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment