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.