Skip to content

Instantly share code, notes, and snippets.

@ajmas
Last active April 29, 2019 21:06
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 ajmas/db31bc5e3c299be2677506814b3f77b8 to your computer and use it in GitHub Desktop.
Save ajmas/db31bc5e3c299be2677506814b3f77b8 to your computer and use it in GitHub Desktop.
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