Skip to content

Instantly share code, notes, and snippets.

@jorgeacortes
Created June 24, 2019 08:58
Show Gist options
  • Save jorgeacortes/6396755a922aff1d492f606d8921a07b to your computer and use it in GitHub Desktop.
Save jorgeacortes/6396755a922aff1d492f606d8921a07b to your computer and use it in GitHub Desktop.
Git pre-push hook to locally reject pushing branches containing 'local/'
#!/bin/sh
# 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
#
# Local branch push disabler.
# branches starting with 'local/' won't be pushed.
# This hook is useful to prevent accidentaly pushing local branches that
# you may create for testing purposes.
current_branch=$(git symbolic-ref HEAD)
policy='[Policy] Do not push local branches! ['$current_branch']\n[Policy] Prevented with pre-push hook.'
if [[ $current_branch == *"local/"* ]]; then
echo -e $policy
exit 1
fi
exit 0
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment