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
mainandstagingso nobody can push to them directly. Add an approval rule, and a CI check that only letsstagingmerge intomain. 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:
- Go to Settings → Repository
- Open the Protected branches section
- Find
mainin 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
# 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:
- In Protected branches, click Add protected branch
- 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:
- Changes move from a feature branch to
mainthrough merge requests. But a Maintainer can still push straight tomain. - You protect
mainandstaging. GitLab now rejects every direct push. - Someone opens a merge request from a feature branch straight to
main. The CI check fails, so it cannot merge. - A merge request from
stagingtomainpasses the CI check. - The team lead approves and merges. This is the only path into
main.
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#
- Go to Settings → Merge requests
- Scroll to Merge request approvals
- 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:
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:
- Go to Settings → Merge requests
- Under Merge checks, turn on:
- Pipelines must succeed
The required flow:
- A developer creates an MR from a feature branch to
develop developmerges tostaging(tested in the staging environment)- The team lead creates an MR from
stagingtomain - CI checks the source branch
- 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:
- Go to Settings → Repository → Protected branches
- For
main, click Edit - 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:
# CODEOWNERS file in the root of your repo
# Everything requires approval from the team lead
* @team-lead-usernameTurn 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:
- Changes move from a feature branch to
mainthrough merge requests. But a Maintainer can still push straight tomain. - You protect
mainandstaging. GitLab now rejects every direct push. - Someone opens a merge request from a feature branch straight to
main. The CI check fails, so it cannot merge. - A merge request from
stagingtomainpasses the CI check. - The team lead approves and merges. This is the only path into
main.
Quick Checklist#
Check that everything is in place:
-
mainbranch protected — push set to “No one” -
stagingbranch 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.