In GitLab, why can a Developer still push directly to a protected main branch, and which protected-branch settings stop it?
answer
- two permissions, not one
- push implies merge, not the reverse
- matching rules: most permissive wins
- say No one, never leave it empty
basics
~20 sA Developer pushes to a protected GitLab branch when any matching rule lists Developers in Allowed to push and merge. Fix it by setting that permission to No one on every matching rule, leaving Allowed to merge for merge requests.
solid answer
~50 sA protected branch in GitLab has two separate permissions. **Allowed to merge** lets a role merge into the branch through a merge request; **Allowed to push and merge** lets a role push commits straight to it and also merge through merge requests. A Developer pushes directly when Developers sit in **Allowed to push and merge** on some rule that matches `main`, for example because the default branch protection was relaxed or a wildcard rule such as `*` grants it, and when several rules match, the most permissive one wins. The fix is **Allowed to merge: Developers + Maintainers** (or Maintainers) and **Allowed to push and merge: No one**, set explicitly on every matching rule, with **Allowed to force push** left off. That matters because anyone who can push can skip the merge request, and with it the approval rules.
go deeper
Recall the two permissions by name and what each allows: merging through a merge request against pushing directly, and that push also implies merge.
Explain how matching rules combine (most permissive for push, merge and force push), why an exact name does not beat a wildcard, and why No one must be chosen explicitly.
Show you audit who holds Allowed to push and merge, including deploy keys and bots, because that permission bypasses merge requests and every approval rule attached to them.
Treat push permission on release branches as a governance decision: who may bypass review, how it is audited, and who may unprotect the branch at all.
## Two permissions, two doors A **protected branch** in GitLab is a branch, or a branch-name pattern, with access controls attached. Its two core permissions are often read as one, and that misreading is how Developers end up pushing to `main`: | Setting | What it allows | |---|---| | **Allowed to merge** | Merge into the branch **through a merge request**, and create new protected branches through the UI and API | | **Allowed to push and merge** | **Push commits directly** to the existing protected branch, and also merge through a merge request | The second permission includes the first: a user in **Allowed to push and merge** can merge merge requests even without **Allowed to merge**. The first does not include the second. Selecting a role grants it to that role and every higher one, so **Developers + Maintainers** covers Developers, Maintainers and Owners. On GitLab Premium and Ultimate you can also list individual users and groups, and **deploy keys** can be added to **Allowed to push and merge** only. Two further toggles sit beside them: - **Allowed to force push**: lets everyone who may push also force push. Off by default. - **Require approval from code owners** (Premium and Ultimate): merge requests into the branch need Code Owner approval, and direct pushes that change files a `CODEOWNERS` rule matches are rejected. ## Why a Developer can still push 1. **Push is granted on the rule itself.** Someone chose **Developers + Maintainers** for **Allowed to push and merge**, often to unblock a release. 2. **The default branch protection was relaxed.** New projects protect the default branch with the instance or group default. The default value, **Fully protected**, stops Developers pushing but still lets Maintainers push; **Partially protected** lets Developers push too. 3. **Another rule matches.** A wildcard rule such as `*` or `m*`, or a group-level rule beside a project rule, also matches `main`. When several rules match, GitLab applies the **most permissive** result for push, merge and force push. An exact name like `main` does **not** override a wildcard. 4. **The push permission was left empty.** GitLab's own documentation disagrees with itself about what an unconfigured push permission allows, and its advice is to select **No one** explicitly rather than leave it unset. ## The configuration that stops it For a branch everyone must reach through review: - **Allowed to merge**: **Developers + Maintainers**, or **Maintainers** if only Maintainers should click Merge. - **Allowed to push and merge**: **No one**, chosen explicitly. - **Allowed to force push**: off. - **Require approval from code owners**: on, if the project uses Code Owners. - Every other rule that matches the branch name carries the same restrictive values, because the most permissive match wins. With that in place no role, Maintainers and Owners included, can push straight to `main`; every change arrives as a merge request. ## Why it matters beyond pushes **Allowed to push and merge is a bypass.** A user who can push does not need a merge request at all, so the merge request's approval rules never get a say. GitLab's Code Owners guidance says the same: that permission is meant for automation and release tooling, and every change from users without it is routed through a merge request. A push permission handed out "temporarily" therefore removes review, not just a convenience. Two related protections are worth naming: - **Deleting** a protected branch is impossible with Git commands; Maintainers and Owners can delete it only in the UI or API. - On Premium and Ultimate, who may **unprotect** a branch can be restricted to named users or groups, configured through the API only; by default Maintainers and Owners can. ## Reading a protection table in an interview When an interviewer hands you a table of rules, work it in this order: 1. List every rule whose name or pattern matches the branch. 2. For push, merge and force push, take the most permissive value across them. 3. For Code Owner approval, take the most restrictive one: if any matching rule requires it, it is required. 4. Check who holds **Allowed to push and merge**, because those users can skip the merge request altogether.
- A GitLab project has a rule for main and a wildcard rule for m*. The main rule says No one may push; m* allows Developers + Maintainers. What can a Developer do on main?Push directly and merge. Both rules match `main`, and GitLab applies the most permissive result for push and merge permissions; an exact branch name does not override a wildcard. To make `main` strict, every matching rule, `m*` included, has to say **No one** for **Allowed to push and merge**.
- In GitLab, why would you ever give a deploy key Allowed to push and merge on a protected branch, and what does that cost?Release tooling sometimes has to push a version bump or a changelog commit straight to the branch, and deploy keys can only be added to **Allowed to push and merge**. The cost is that the key bypasses merge requests, so its pushes skip approval rules; keep it narrow, audited and limited to the branches it needs.
Allowed to merge is a pass that only opens the front door, where reception checks the visitor log; Allowed to push and merge also opens the side door that walks straight past reception.
- Allowed to merge in GitLab also lets a user push commits straight to the branch
- An exact rule for main always overrides a wildcard rule that also matches it
- Leaving Allowed to push and merge empty is the same as choosing No one
- Fully protected stops everyone, Maintainers included, from pushing to the default branch
- Users who can push to a protected branch still need approvals for their pushes