Skip to content

Instantly share code, notes, and snippets.

@kyokley
Last active May 17, 2018 18:51
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 kyokley/ad3c5ed867d3b9de4487a24540c7b269 to your computer and use it in GitHub Desktop.
Save kyokley/ad3c5ed867d3b9de4487a24540c7b269 to your computer and use it in GitHub Desktop.
Git Pre-push Hook

Since I always forget how to write a git hook to prevent pushing certain branches to remotes, I'm writing this gist to, hopefully, help my future self remember...

To prevent local branch "work" from being pushed to the github remote:

#!/bin/sh

remote="$1"
url="$2"

while read local_ref local_sha remote_ref remote_sha
do
    if [ "$local_ref" = "refs/heads/work" ]
    then
        if [ "$remote" = "github" ]
        then
            echo "Can't push work branch to github"
            exit 1
        fi
    fi
done

exit 0

In order to determine the ref of the local branch you are using:

git show-ref

When done writing the file, name it pre-push, save it in .git/hooks, and be sure to make it executable.

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