Protect the `master` branch - MacDreamy/workable GitHub Wiki

You must create pre-commit hook which rejects commits to master branch. Git doesn't call pre-commit hook when you call merge command, so this hook will be rejecting only regular commits.

Go to your repository. Create file .git/hooks/pre-commit with following content:

#!/bin/sh

branch="$(git rev-parse --abbrev-ref HEAD)"

if [ "$branch" = "master" ]; then
  echo "You can't commit directly to master branch"
  exit 1
fi

Make it executable (not required on Windows):

$ chmod +x .git/hooks/pre-commit

To disable fast-forward merges you must also add following option to your .git/config file:

[branch "master"]
    mergeoptions = --no-ff

If you want also protect master branch on your remote, check this answer: How to restrict access to master branch on git