DON’T PUSH THAT BRANCH!

Something that happens to must developers is that need to have some stuff that can’t be pushed and needs to be committed, and you really can’t push it to a server. So how do you force git to restrict yourself from pushing that branch?

#!/usr/bin/env bash
declare -a protected_branches=("protected")
declare current_branch
current_branch=$(git symbolic-ref HEAD | sed -e 's,.*/\(.*\),\1,')
for branch in "${protected_branches[@]}"; do
if [ "$branch" = "$current_branch" ]; then
echo "This is a protected branch, BAD PUPPY!"
exit 1
fi
done

You put that into .git/hooks/pre-push, give it chmod +x and it will tell you BAD PUPPY each time you try to push that protected branch.

Since protected_branches is an array you can protect several branches in the same repo. Enjoy, puppy.