You can use a pre-commit hook.
For example, place the following script as .git/hooks/pre-commit
:
#!/bin/bash
if test $(git rev-parse --abbrev-ref HEAD) = "master" ; then
echo "Cannot commit on master"
exit 1
fi
And set it as executable
chmod +x .git/hooks/pre-commit