#!/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` # 2. Set executable permissions, run `chmod +x .git/hooks/pre-push` # 3. Or, use `rake hooks:pre_push` to install # # Try a force push to any branch, 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 will 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: # # # See Sample pre-push script: # # # This script is based on: # https://gist.github.com/pixelhandler/5718585 current_branch=$(git symbolic-ref HEAD | sed -e 's,.*/\(.*\),\1,') policy='[Policy] Never force push or delete the '$current_branch' branch! (Prevented with pre-push hook.)' push_command=$(ps -ocommand= -p $PPID) is_destructive='force|delete|\-f' will_remove_current_branch=':'$current_branch do_exit(){ echo $policy exit 1 } if [[ $push_command =~ $is_destructive ]] && [ $1 = 'origin' ]; then do_exit fi if [[ $push_command =~ $is_destructive ]] && [[ $1 = 'origin' ]]; then do_exit fi if [[ $push_command =~ $will_remove_current_branch ]]; then do_exit fi unset do_exit exit 0