Skip to content

Instantly share code, notes, and snippets.

@ajmas ajmas/pre-push
Last active Apr 29, 2019

Embed
What would you like to do?
Git 'pre-push' script to prevent pushing to a given branch on a remote
#!/bin/bash
# Simple 'pre-push' script to prevent an accidental push to a
# specific branch on a specific remote. Mainly needed when a
# remote branch is not protected.
#
# Place this in the .git/hooks folder of your project and then
# ensure you make it executable. Modify according to your needs.
restricted_branch=master
restricted_remote=upstream
remote=$1
current_branch=$(git symbolic-ref HEAD | sed -e 's,.*/\(.*\),\1,')
# escape sequence info:
# - https://stackoverflow.com/questions/4842424/list-of-ansi-color-escape-sequences
# - http://ascii-table.com/ansi-escape-sequences-vt-100.php
bold="\033[36m"
warning="\033[33;1m"
normal="\033[0m" #$(tput sgr0)
echo -e "Pushing ${bold}${current_branch}${normal} on ${bold}${remote}${normal}"
if [ "$remote" = "$restricted_remote" ] && [ "$current_branch" = "$restricted_branch" ]; then
echo -e "${warning}Aborting: Pushing to ${restricted_branch} on ${restricted_remote} is disabled${normal}"
# deny push
exit 1
else
# allow push
exit 0
fi
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.