Sub topic 10
How overlapping rulesets combine
This chapter assumes you can read a ruleset JSON body, know how organisation and repository rulesets differ, and have seen the rule types (pull request, status checks, required workflows). It covers what happens when several rulesets apply to the same branch.
Rulesets add up, they do not compete
In classic branch protection, one rule matched a branch and that rule was the whole policy. Rulesets work differently. Every active ruleset whose conditions match a branch contributes its rules, and GitHub evaluates all of them together. No ruleset has priority over another, and there is no ordering to manage.
Rulesets can come from several places:
- An organisation ruleset that matches the repository and the branch.
- One or more repository rulesets in that repository.
- Classic branch protection rules on the same branch, if any still exist.
A push or merge must satisfy the combined rules from all of these. Adding a ruleset can only make a branch stricter. It can never loosen what another ruleset requires.
How each rule type combines
When two rulesets define the same rule type, GitHub applies the most restrictive version. What "most restrictive" means depends on the rule type.
| Rule type | Combined result |
|---|---|
| `deletion`, `non_fast_forward`, `required_linear_history`, `required_signatures` | Enforced if any ruleset has it. These rules have no parameters, so there is nothing to merge. |
| `pull_request` approval count | The highest `required_approving_review_count` |
| `pull_request` boolean options | Enabled if any ruleset enables it, such as `require_code_owner_review` or `dismiss_stale_reviews_on_push` |
| `required_status_checks` | The union of all required checks. Every listed check must pass. |
| `workflows` | The union of all required workflows. Every listed workflow must pass. |
| Pattern rules, such as commit message patterns | Every pattern must match. A commit must satisfy all of them. |
Two consequences are worth remembering. First, requiring a status check in two rulesets does not make it count twice. It is a single requirement. Second, a repository ruleset cannot lower an organisation requirement. If the organisation requires one approval and a repository ruleset asks for zero, the pull request still needs one.
A worked example
Suppose the organisation applies the protect-default-branch ruleset from the earlier chapters to every repository. It requires one approving review, requires code owner review, blocks deletion and blocks force pushes.
The owners of one repository, payments-api, add their own ruleset to the default branch:
{
"name": "payments-stricter-reviews",
"target": "branch",
"enforcement": "active",
"bypass_actors": [],
"conditions": {
"ref_name": {
"include": ["~DEFAULT_BRANCH"],
"exclude": []
}
},
"rules": [
{
"type": "pull_request",
"parameters": {
"required_approving_review_count": 2,
"dismiss_stale_reviews_on_push": false,
"require_code_owner_review": false,
"require_last_push_approval": true,
"required_review_thread_resolution": false
}
},
{
"type": "required_linear_history"
}
]
}The effective policy on the default branch of payments-api is:
- Two approving reviews. The higher count wins over the organisation's one.
- Code owner review required. The repository ruleset sets it to
false, but the organisation ruleset sets it totrue, andtrueis the stricter value. - Stale approvals dismissed on push. Same reason: the organisation ruleset enables it.
- Approval of the most recent push required, because only the repository ruleset enables it.
- Review thread resolution required, because only the organisation ruleset enables it.
- Linear history required, from the repository ruleset.
- Deletion and force pushes blocked, from the organisation ruleset.
The repository ruleset's false values did not turn anything off. A false in one ruleset means "this ruleset does not ask for it", not "forbid it".
Bypass applies per ruleset
Bypass is the part of layering that surprises people most. A bypass actor is listed inside one ruleset, and it bypasses only that ruleset.
Consider the organisation ruleset from earlier, where repository admins can bypass through a pull request. In payments-api, an admin opens a pull request with one approval. The organisation ruleset allows this for admins, which only means they can merge despite its requirements. The repository ruleset has an empty bypass_actors list, so its two-approval requirement still blocks the merge.
To let a role bypass the combined policy, you must add it to every ruleset that matters. This is a useful property. A repository team can keep a strict ruleset that the organisation's emergency bypass does not reach. It also means that when someone says "admins can bypass", the next question is "in which rulesets?".
Bypass modes are also evaluated per ruleset:
alwayslets the actor push directly or merge without conditions from that ruleset.pull_requestlets the actor bypass only when acting through a pull request.
Enforcement status in layers
Only rulesets with enforcement set to active contribute rules that block anything.
active: the ruleset blocks violations.evaluate: the ruleset blocks nothing. It records what would have been blocked in rule insights.disabled: the ruleset is ignored.
Because rulesets add up, evaluate is a safe way to test a stricter ruleset on top of an existing active one. The active rulesets keep enforcing. The evaluating ruleset only reports the extra violations it would cause. Evaluate mode is available for organisation rulesets and, depending on your plan, for repository rulesets.
Interaction with classic branch protection
Rulesets and branch protection rules do not replace each other. If both match a branch, both apply, and a change must satisfy both. The stricter value of each setting effectively wins, as with two rulesets.
This matters during migration. If you create a ruleset that mirrors a branch protection rule, nothing visibly changes, and any stale branch protection rule keeps working underneath. If you later loosen the ruleset, the old branch protection rule can still block people. When troubleshooting an unexpected block, check branch protection in addition to the ruleset list.
Inspecting the effective rules
You do not have to work out the merge by hand. The REST API returns the active rules that apply to a branch, with the ruleset each one came from:
gh api \
-H "Accept: application/vnd.github+json" \
/repos/YOUR_ORG/payments-api/rules/branches/main \
--jq '.[] | {type, ruleset_source, ruleset_source_type, ruleset_id}'Each entry names its type and the ruleset that supplies it. For payments-api you would see two pull_request entries, one from the organisation ruleset and one from the repository ruleset. The API lists each contributing rule separately, so read the parameters of every entry. The merge happens when GitHub evaluates the change, not in this response.
To see all rulesets that could apply to a repository, including those inherited from the organisation, list them with includes_parents:
gh api \
"/repos/YOUR_ORG/payments-api/rulesets?includes_parents=true" \
--jq '.[] | {id, name, source_type, enforcement}'Common errors
- Expecting a repository ruleset to relax an organisation ruleset. It cannot. To exempt a repository, change the organisation ruleset's
repository_namecondition (for example, exclude it by name) or add a bypass actor there. - Adding a bypass actor in only one place. The actor still hits the other rulesets. List every ruleset that applies and check each one.
- Reading
falseas "off". Afalseoption in one ruleset has no effect when another ruleset sets it totrue. - Forgetting overlapping required workflows. If two rulesets require different workflows, both must pass. A merge can stall on a workflow that nobody on the repository team knows an organisation ruleset requires.
- Overlooking old branch protection. A block that no ruleset explains often comes from a legacy rule.
- Testing in
evaluatemode and expecting a block. Evaluate mode never blocks. Check the insights instead.
Design guidance
Keep organisation rulesets for baseline requirements that must hold everywhere, and let repository rulesets only add requirements. If you find a repository needing a weaker rule than the baseline, the baseline's conditions are probably too broad, so narrow them instead of layering around them. Give each ruleset a name that says what it is for, such as protect-default-branch or payments-stricter-reviews, so the sources shown in the API output are readable.
Exercise
The organisation ruleset requires 2 approving reviews and sets requirecodeownerreview to false. A repository ruleset on the same default branch requires 1 approving review and sets requirecodeownerreview to true. Both are active. What must a pull request satisfy to merge, and can a repository admin listed as a bypass actor only in the organisation ruleset merge with one non-code-owner approval?
Show answer
The pull request needs 2 approving reviews (the higher count) and code owner review (true is stricter than false). A repository admin who is a bypass actor only in the organisation ruleset cannot merge with one non-code-owner approval. Bypass applies per ruleset, so the repository ruleset's requirements, including code owner review, still block the merge.
Exercise
You want to trial a new ruleset that requires linear history on every repository's default branch, without blocking anyone yet. Which enforcement status do you use, and why is it safe next to the existing active rulesets?
Show answer
Use evaluate. Rulesets combine by adding rules, and an evaluate ruleset enforces nothing. It only records would-be violations in rule insights. The existing active rulesets keep enforcing as before. When the insights show an acceptable level of violations, switch the ruleset to active.