Authored by Peter Weinert

Prevent commiting on protected branches (e.g. master)


First protect those branches on your GitLab repository:

  • Find Project Settings - Repository - Protected Branches and press Expand.
  • Choose a branch like "master" in the drop-down list "Branch".
  • Set "Allow to push" to "No one".

Now you can protect those branches in your local repository as well:

  • Add this snippet to your executable script .git/hooks/pre-commit.


You're prevented locally from committing to protected branches, i.e. master or develop. Open a Merge Request in GitLab instead.

Source code

pre-commit 513 Bytes
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment