I would like to protect my git repository so only non master branches can be overwritten. Is there a way to protect only selected branches?
This SO answer will give you what you're looking for. Just edit it to apply to the master branch instead:
#!/bin/sh
# lock the master branch for pushing
refname="$1"
if [ "$refname" = "refs/heads/master" ]
then
echo "XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX"
echo "You cannot push to the master branch."
echo "XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX"
exit 1
fi
exit 0
Update:
This will prevent all pushes to the master branch, including fast-forward.