Sub topic 13
Moving from branch protection rules to rulesets
This chapter covers how to replace classic branch protection rules with rulesets without leaving a branch unprotected during the change. It assumes you know the earlier chapters: how a ruleset is built from targets, rules, bypass actors and an enforcement status, and how overlapping rulesets combine. It also assumes you can read the gh CLI and JSON examples.
Why migrate at all
Branch protection rules still work, and GitHub has not removed them. Rulesets add capabilities that branch protection lacks:
| Capability | Branch protection | Rulesets |
|---|---|---|
| Scope | One repository at a time | One repository, or many repositories through an organisation ruleset |
| Targets | Branches only | Branches and tags, plus push rulesets for paths, sizes and extensions |
| Several policies on one branch | One rule per matching pattern, set up repository by repository | Many rulesets layer, and the strictest result applies |
| Bypass | Mostly "administrators or not", plus a few per-setting exceptions | An explicit bypass list of roles, teams and apps, with `always` or `pull_request` mode |
| Trial run | None | `evaluate` enforcement shows what would be blocked |
| Visibility | Mostly visible to admins | Readable by anyone with read access to the repository |
| Required workflows | Not available | Available in organisation rulesets |
The last row is the reason this tutorial covers migration. You cannot require a shared workflow to pass across repositories with branch protection. If your organisation-wide policy depends on that, you must move at least part of your protection to rulesets.
Map each setting to a rule
There is no automatic converter. You translate each branch protection setting by hand. Most settings have a direct counterpart.
| Branch protection setting | Ruleset rule or option |
|---|---|
| Require a pull request before merging | `pull_request` |
| Required approvals | `required_approving_review_count` in `pull_request` |
| Dismiss stale approvals on new commits | `dismiss_stale_reviews_on_push` |
| Require review from Code Owners | `require_code_owner_review` |
| Require approval of the most recent push | `require_last_push_approval` |
| Require conversation resolution | `required_review_thread_resolution` |
| Require status checks | `required_status_checks` |
| Require branches to be up to date | `strict_required_status_checks_policy` |
| Require signed commits | `required_signatures` |
| Require linear history | `required_linear_history` |
| Require merge queue | `merge_queue` |
| Allow force pushes (off) | `non_fast_forward` |
| Allow deletions (off) | `deletion` |
| Restrict who can push | `update`, combined with a bypass list |
| Do not allow bypassing the above settings | An empty bypass list |
| Allow specified actors to bypass pull requests | A bypass actor in `pull_request` mode |
Two translations need care.
"Restrict who can push" is an inversion. Branch protection lists who may push. A ruleset with the update rule blocks everyone, and the bypass list names who is exempt. The result is similar, but the model is different. Teams that were allowed to push now appear as bypass actors, and you should consider whether pull_request mode is enough for them.
"Include administrators" becomes bypass configuration. In branch protection, leaving "Do not allow bypassing" unticked lets administrators skip the rules. In a ruleset, nobody bypasses unless you add them. Administrators lose their implicit exemption in the new ruleset. That is usually what you want, but it surprises people during incidents. Add the repository admin role as a pull_request bypass actor if you want a narrow emergency path.
Inventory what you have
Start by reading the current settings of one repository. This command prints the settings that matter for translation:
gh api /repos/YOUR_ORG/YOUR_REPO/branches/main/protection \
--jq '{
approvals: .required_pull_request_reviews.required_approving_review_count,
code_owners: .required_pull_request_reviews.require_code_owner_reviews,
checks: .required_status_checks.contexts,
up_to_date: .required_status_checks.strict,
linear: .required_linear_history.enabled,
force_push: .allow_force_pushes.enabled,
deletions: .allow_deletions.enabled,
admins_enforced: .enforce_admins.enabled
}'A field that prints null is a setting that is not configured. To find which repositories have protection on their default branch, loop over the organisation:
gh repo list YOUR_ORG --no-archived --limit 200 \
--json name,defaultBranchRef \
--jq '.[] | select(.defaultBranchRef != null) | "\(.name) \(.defaultBranchRef.name)"' |
while read -r repo branch; do
if gh api "/repos/YOUR_ORG/$repo/branches/$branch/protection" --silent 2>/dev/null; then
echo "protected: $repo ($branch)"
else
echo "no classic protection: $repo ($branch)"
fi
doneThe second label is approximate. The call also fails on permission errors or on plans that do not support protection for private repositories, so check the failures by hand before you treat a repository as unprotected.
Group the repositories by how similar their settings are. Repositories that share the same approvals, checks and force-push settings can be covered by one organisation ruleset. Repositories with unusual settings keep a repository-level ruleset, or stay on branch protection until you have time to handle them.
Write the replacement
Suppose the inventory shows this configuration: one approval, a status check named build, up-to-date branches required, no force pushes, no deletions. The matching ruleset body is:
{
"name": "default-branch-baseline",
"target": "branch",
"enforcement": "evaluate",
"bypass_actors": [],
"conditions": {
"ref_name": {
"include": ["~DEFAULT_BRANCH"],
"exclude": []
},
"repository_name": {
"include": ["~ALL"],
"exclude": ["sandbox-*"]
}
},
"rules": [
{ "type": "deletion" },
{ "type": "non_fast_forward" },
{
"type": "pull_request",
"parameters": {
"required_approving_review_count": 1,
"dismiss_stale_reviews_on_push": false,
"require_code_owner_review": false,
"require_last_push_approval": false,
"required_review_thread_resolution": false
}
},
{
"type": "required_status_checks",
"parameters": {
"strict_required_status_checks_policy": true,
"required_status_checks": [
{ "context": "build" }
]
}
}
]
}Send it to the organisation endpoint, as in the earlier chapter on the API:
gh api \
--method POST \
-H "Accept: application/vnd.github+json" \
/orgs/YOUR_ORG/rulesets \
--input default-branch-baseline.jsonThe ruleset starts in evaluate mode. Evaluate mode records what the ruleset would have blocked without blocking anything. Availability of evaluate mode, especially for organisation rulesets, depends on your GitHub plan, so confirm it in the ruleset settings first. If it is not available, create the ruleset as disabled, or target a small set of test repositories before you widen it.
A common mistake is to copy a status check name that does not match the name the workflow reports. The context value must equal the check name shown on a pull request, which for GitHub Actions is normally the job name. A mismatch leaves the check "expected" forever, and merges block.
Cut over in stages
Follow this order. Each step can be reversed until the last one.
- Evaluate. Leave the classic rules in place and let the ruleset run in
evaluatemode for a few days of normal activity. - Review insights. Open rule insights and look for entries the ruleset would have blocked while the classic rules allowed them. Each one is either a translation error or a rule that is now stricter than before.
- Activate. Change
enforcementtoactive. Both systems now protect the branch, so behaviour should not change. - Verify. Ask the API which rules apply to the branch:
gh api /repos/YOUR_ORG/YOUR_REPO/rules/branches/main \
--jq '.[] | {type: .type, source: .ruleset_source, source_type: .ruleset_source_type}'Every rule you translated should appear, with the ruleset as its source.
- Remove the classic rule. Delete it in the repository settings, or per repository with the API:
gh api --method DELETE /repos/YOUR_ORG/YOUR_REPO/branches/main/protectionThis removes protection settings, and it is hard to restore exactly. Save the JSON from the inventory step first.
Failure modes to expect
- Double enforcement. If you skip step 5, the stricter of the two sets wins. A classic rule that requires two approvals will override a ruleset that requires one, and nothing in the ruleset explains why.
- Lost exemptions. An administrator who relied on the implicit bypass now sees a block. Decide on bypass actors deliberately before activation, and prefer
pull_requestmode overalways. - Pattern differences. Classic rules match by name pattern per repository. Rulesets add
~DEFAULT_BRANCHand~ALL. If your classic rule usedmainand a repository's default branch istrunk, a~DEFAULT_BRANCHruleset protects a different branch than before. - Renamed checks. Reusing a check name that no longer exists blocks all merges. Test with one pull request in evaluate mode before activating.
- Tag and push gaps. Classic protection never covered tags or file paths. Migration is a good moment to add those rules, but add them as separate rulesets so you can roll each back independently.
Exercise
A repository has a classic rule on main requiring two approvals. You create an active ruleset on the same branch requiring one approval, then see that pull requests still need two. Why, and what is the correct fix?
Show answer
Classic branch protection and rulesets both apply, and the stricter requirement wins, so the classic rule's two approvals still hold. The fix is to remove or edit the classic rule once the ruleset is verified. If two approvals is the intended policy, change the ruleset to require two so that the ruleset states the real requirement.
Exercise
Your classic rule uses "Restrict who can push" with a release team listed. How do you express this in a ruleset?
Show answer
Add the update rule so that updates to the branch are blocked for everyone, then add the release team to bypass_actors. Use pull_request bypass mode if the team should still go through a pull request, or always only if the team must push directly.