Skip to content
subuilds.dev

← logbuild #14 passed185f99d

GitLab Branch Protection: Lock Down Main and Control Who Can Merge

Step-by-step guide to protecting your main branch in GitLab. Allow merges only from staging, limit who can approve, and require a merge request for every change.

· 8 min read

Your branches are in place and staging works. But anyone with Maintainer access can still push directly to main. This guide fixes that.

It locks main so that:

  • No one pushes directly to it
  • Merges only come from staging
  • Only a specific person can approve and merge

In Simple Words#

The problem
Anyone with Maintainer access can push code straight to main. One bad push puts broken code in production.
The root cause
Nothing forces a change to go through a merge request, a review and the CI checks. A change can skip staging, and nobody has to approve it.
The solution
Protect main and staging so nobody can push to them directly. Add an approval rule, and a CI check that only lets staging merge into main. Then let one chosen person merge.
Think of it like this
Your finished work is in a room. Today many people have a key and can walk in. After this guide there is one door with a guard. The guard only lets in work that passed the test room and has a signature.

Why Protect the Main Branch?#

main is production. Every commit there should be tested, reviewed, and made on purpose. Without protection:

  • A developer can push broken code to production by mistake
  • Hotfixes (urgent fixes) skip staging
  • There is no record of who approved a change

Branch protection makes every change go through a merge request (MR), an approval, and CI checks. A merge request asks to merge one branch into another, so someone can review it first.

Step 1: Protect the Main Branch#

In your GitLab project:

  1. Go to Settings → Repository
  2. Open the Protected branches section
  3. Find main in the list (or add it)

Set these values:

Setting Value
Allowed to merge Maintainers
Allowed to push and merge No one
Allowed to force push Off

Result:

  • No one can push directly to main, not even Maintainers
  • Only Maintainers can merge, and only through merge requests
  • Force push is off, so nobody can overwrite the history
bash
# This will now be rejected for everyone:
git push origin main
# remote: GitLab: You are not allowed to push code to protected branches on this project.

Step 2: Protect the Staging Branch Too#

Do the same for staging:

  1. In Protected branches, click Add protected branch
  2. Select or type staging
Setting Value
Allowed to merge Maintainers
Allowed to push and merge No one
Allowed to force push Off

Both branches are now protected. All changes move through merge requests. The diagram shows the full path that this guide builds:

  1. Changes move from a feature branch to main through merge requests. But a Maintainer can still push straight to main.
  2. You protect main and staging. GitLab now rejects every direct push.
  3. Someone opens a merge request from a feature branch straight to main. The CI check fails, so it cannot merge.
  4. A merge request from staging to main passes the CI check.
  5. The team lead approves and merges. This is the only path into main.
5 / 5

No shortcuts.

Step 3: Require Merge Request Approvals#

Branch protection stops direct pushes. It does not require a review. Add approvals for that.

Project-Level Approval Rules#

  1. Go to Settings → Merge requests
  2. Scroll to Merge request approvals
  3. Click Add approval rule
Field Value
Rule name Production Gatekeeper
Approvals required 1
Approvers Select your team lead or release manager

Every MR now needs approval from the chosen person before it can be merged.

Prevent Approval by the Author#

In the same section, turn on:

  • Prevent approval by author — authors can’t approve their own MRs
  • Prevent editing approval rules in merge requests — developers can’t remove the rule
  • Remove all approvals when commits are pushed — new commits reset approvals

These settings close the common ways around the rule.

Step 4: Restrict Which Branches Can Merge to Main#

GitLab has no built-in setting for “only allow merges from branch X”. Use a CI check instead.

Add this to your .gitlab-ci.yml:

yaml
check-source-branch:
  stage: .pre
  script:
    - |
      if [ "$CI_MERGE_REQUEST_TARGET_BRANCH_NAME" = "main" ]; then
        if [ "$CI_MERGE_REQUEST_SOURCE_BRANCH_NAME" != "staging" ]; then
          echo "❌ Error: Only the 'staging' branch can be merged into 'main'."
          echo "Source branch: $CI_MERGE_REQUEST_SOURCE_BRANCH_NAME"
          echo "Please merge your changes to staging first."
          exit 1
        fi
        echo "✅ Source branch is 'staging' — allowed to merge to main."
      fi
  rules:
    - if: $CI_PIPELINE_SOURCE == "merge_request_event"

Any MR to main from a branch other than staging fails at once.

Require Pipeline to Pass Before Merging#

Make this check block merges:

  1. Go to Settings → Merge requests
  2. Under Merge checks, turn on:
    • Pipelines must succeed

The required flow:

  1. A developer creates an MR from a feature branch to develop
  2. develop merges to staging (tested in the staging environment)
  3. The team lead creates an MR from staging to main
  4. CI checks the source branch
  5. The team lead approves and merges

Any other path to main is blocked.

Step 5: Limit Who Can Merge to Main#

You have limited what can merge. Now limit who.

Using Allowed to Merge Setting#

Step 1 set “Allowed to merge” to Maintainers. To limit it to a specific person:

  1. Go to Settings → Repository → Protected branches
  2. For main, click Edit
  3. Under Allowed to merge, select one user instead of a role

Now only the chosen release manager can merge to main, even if there are several Maintainers.

Using CODEOWNERS (GitLab Premium)#

On GitLab Premium or Ultimate, use a CODEOWNERS file:

text
# CODEOWNERS file in the root of your repo

# Everything requires approval from the team lead
* @team-lead-username

Turn on Require CODEOWNERS approval in Settings → Repository → Protected branches.

The code owner must approve before any merge can happen.

Putting It All Together#

Summary of the protections:

Protection What It Does
Protected branches No direct pushes to main or staging
Merge request approvals Team lead must approve before merge
CI source branch check Only staging (or hotfix/*) can merge to main
Pipeline must succeed CI must pass before merge is allowed
Specific merge access Only the designated person can click merge

The complete flow:

  1. Changes move from a feature branch to main through merge requests. But a Maintainer can still push straight to main.
  2. You protect main and staging. GitLab now rejects every direct push.
  3. Someone opens a merge request from a feature branch straight to main. The CI check fails, so it cannot merge.
  4. A merge request from staging to main passes the CI check.
  5. The team lead approves and merges. This is the only path into main.
5 / 5

Quick Checklist#

Check that everything is in place:

  • main branch protected — push set to “No one”
  • staging branch protected
  • Approval rule created with specific approver
  • “Prevent approval by author” enabled
  • “Remove all approvals when commits are pushed” enabled
  • CI job checks source branch for MRs targeting main
  • “Pipelines must succeed” enabled
  • Specific user set as allowed to merge (not just “Maintainers”)

Summary#

This takes 10 minutes. After that, every production change is planned, reviewed, and tested. Branch protection + approval rules + CI checks make a gate that is hard to pass by mistake. No single one of them is enough alone.

If you have not set up your branching strategy yet, start with the Git Branching Strategy: A Practical Case guide. This post builds on it.