In GitLab Premium, how would you guarantee that nothing reaches main without two approvals, one from a code owner, while developers can still merge their own approved merge requests?
answer
- branch protection plus approval rules
- nobody pushes, developers merge
- code owner toggle on the branch
- lock the rules against overrides
- audit every rule that matches
basics
~20 sProtect main with Allowed to merge: Developers + Maintainers, Allowed to push and merge: No one and Require approval from code owners; add a two-approval rule targeting main; then lock self-approval and per-merge-request rule edits in the approval settings.
solid answer
~50 sIt takes both halves of GitLab's controls. On the **protected branch** `main`: **Allowed to merge: Developers + Maintainers** so developers merge their own merge requests, **Allowed to push and merge: No one** chosen explicitly so nobody can bypass review by pushing, **Allowed to force push** off, and **Require approval from code owners** on, with a `CODEOWNERS` file covering the code. In the **approval rules**, a rule targeting `main` with **Approvals required: 2**; a code owner who is also eligible for that rule counts toward both, so two people suffice when one is a code owner. In the **approval settings**: **Prevent approval by merge request creator**, **Prevent approvals by users who add commits**, **Prevent editing approval rules in merge requests**, and remove approvals when commits are added. Then audit every rule that matches `main`, because the most permissive wins, and every rule's eligible approvers, because an unsatisfiable rule is auto-approved.
go deeper
Recall that protecting the branch and requiring approvals are two separate settings, and that both are needed for review to be enforced.
Give the exact values: Allowed to merge for developers, Allowed to push and merge set to No one, the Code Owner toggle and a two-approval rule targeting main.
Close the leaks you have seen in production: self-approval, per-merge-request rule edits, wildcard rules granting push, bots with push access and auto-approved rules.
Own the governance model: enforce settings at the top-level group or instance, restrict who can unprotect branches, and decide which exceptions are allowed and how they are audited.
## Two halves of one guarantee In GitLab, "nothing reaches `main` unreviewed" needs two mechanisms working together: - **Protected branches** decide *who may change the branch and how*: by pushing directly or only by merging a merge request. - **Merge request approvals** decide *what a merge request needs before it can merge*: how many approvals, from whom. Either alone fails. Approval rules mean nothing to someone who can push straight to `main`, and branch protection alone lets any developer merge their own unreviewed merge request. On GitLab Free, approvals are optional, so this design needs **Premium** or **Ultimate**; Code Owner approval is Premium and Ultimate as well. ## Step 1: protect main so every change is a merge request | Setting | Value | Why | |---|---|---| | **Allowed to merge** | **Developers + Maintainers** | Developers can merge their own merge requests once approved | | **Allowed to push and merge** | **No one**, chosen explicitly | Anyone who can push skips the merge request and its approvals | | **Allowed to force push** | Off | No history rewrites on `main` | | **Require approval from code owners** | On | Merge requests into `main` need Code Owner approval per matched rule | Add a `CODEOWNERS` file. GitLab uses the first one it finds in `./CODEOWNERS`, `./docs/CODEOWNERS` or `./.gitlab/CODEOWNERS`. A section can demand more than one Code Owner approval by appending a count, as in `[Payments][2]`. ## Step 2: require two approvals Create an approval rule with **Approvals required** set to `2` and its **Target branch** set to `main`, with eligible approvers who are direct members of the project or of the groups you name (inherited members of an approver group cannot approve). When an eligible approver approves, the approval counts toward **every** rule that person belongs to, so a code owner who is also eligible for the two-approval rule satisfies the Code Owner requirement and one of the two approvals at once. That is exactly "two approvals, one from a code owner". ## Step 3: close the self-approval holes In the approval settings: - **Prevent approval by merge request creator**: on by default; keep it on. - **Prevent approvals by users who add commits**: turn it on, so a co-author cannot be one of the two. - **Prevent editing approval rules in merge requests**: turn it on, or an author can lower the rule on their own merge request. - **Remove all approvals** when commits are added (the default), or **Remove approvals by Code Owners if their files changed** if a full reset is too noisy. Set them at the **top-level group** so projects inherit them and Maintainers cannot switch them off; on Self-Managed or Dedicated an administrator can lock them for the whole instance. ## Step 4: audit for the leaks 1. **Other matching rules.** A wildcard such as `*` or a group-level rule that also matches `main` and grants push wins, because GitLab applies the most permissive match for push and merge. An exact `main` rule does not override it. Code Owner approval is the exception: the most restrictive match applies. 2. **Who holds push.** Deploy keys, bots and release tooling in **Allowed to push and merge** skip the merge request and its approval rules; with Code Owner approval on, only their pushes that touch owned paths are refused. Keep that list empty or tiny and reviewed. 3. **Unsatisfiable rules.** A rule with no eligible approvers, or whose only eligible approver is the author, is marked **Auto approved** and stops blocking. Check that each rule has enough real approvers. 4. **Who can unprotect.** By default Maintainers and Owners can unprotect a branch; on Premium and Ultimate the API can restrict that to named users or groups. ## Optional but common additions - **Pipelines must succeed** and **All threads must be resolved**, so approved code also passes CI and has no open questions. - On Ultimate, a security team may add a merge request approval policy, whose rules projects cannot weaken; that is security-policy territory rather than repository settings. The interview answer that lands names both halves, gives the exact values for the two permissions, and then lists the leaks: wildcard rules, push holders, rule overrides and auto-approved rules.
- After this GitLab setup, a release bot still lands commits on main with no approvals. Where do you look first?At who holds **Allowed to push and merge** on every rule matching `main`, including wildcard and group-level rules, and at deploy keys added to it. A push permission anywhere among the matching rules wins and lets the bot skip the merge request entirely. Either route the bot through merge requests or keep its push access as a documented, audited exception.
- In this GitLab setup, why does it matter that approvers from a group must be direct members?Only direct members of an approver group are eligible; inherited members cannot approve. If the named group's real reviewers are inherited members, the rule may have no eligible approvers, and GitLab marks such a rule **Auto approved**, silently removing one of the two approvals. Add direct members or share the project with the right group.
- Why choose Developers + Maintainers for Allowed to merge here rather than Maintainers only?The requirement is that developers merge their own approved work. Approvals and Code Owner rules already decide whether a merge request may merge; **Allowed to merge** only decides who clicks the button. Restricting it to Maintainers adds a bottleneck without adding review, as long as nobody holds push.
- Two required approvals on main are enough even if Developers can push to it
- An exact rule for main overrides a wildcard rule that grants Developers push
- A code owner's approval cannot also count toward the general two-approval rule
- Approval rules on GitLab Free already block merges until approved
- An approval rule with no eligible approvers keeps the merge request blocked