Yes, it is possible. You must create a pre-commit hook which rejects commits to the master branch. Git doesn’t call a pre-commit hook when you call the merge command, so this hook will be rejecting only regular commits.
-
Go to your repository.
-
Create a file, .git/hooks/pre-commit, with the 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 the following option to your .git/config file:
[branch "master"]
mergeoptions = --no-ff
If you want also protect the master branch on your remote, check this answer: How to restrict access to master branch in Git