Sub topic 16
Choosing between branch protection and rulesets, and migrating
This chapter assumes you know how classic branch protection rules work, how rulesets target refs, and how evaluate mode and bypass actors behave. It helps you decide which system to use and how to move from one to the other.
How the two systems differ
Both systems enforce the same core controls on a branch. The differences are in scope and in how you manage them.
| Aspect | Branch protection rules | Rulesets |
|---|---|---|
| Targets | Branches only, by name pattern | Branches and tags, by include and exclude patterns |
| Layering | One rule per pattern, and several matching rules apply to the same branch | Many rulesets can apply at once, and the strictest result wins |
| Exemptions | The administrator switch (`enforce_admins`) | A bypass list of named actors |
| Dry run | None | `evaluate` enforcement status |
| Scope | One repository | One repository or a whole organization |
| Visibility | Visible to people with admin access | Readable by anyone with read access to the repository |
Rulesets also add rules that classic protection lacks, such as tag protection and push rules.
Decision criteria
Choose rulesets when any of these is true:
- You manage many repositories and want one policy applied across them.
- You need to protect tags or restrict pushes.
- You want to test a rule in evaluate mode before it blocks anyone.
- You need exemptions for specific teams or apps, not all administrators.
Keep classic rules when a single repository already works well with them and no one needs these features. Mixing the two is allowed, but it makes the effective policy harder to read, because you must combine both sources to know what applies.
Migrating safely
Migrate in stages so you never leave a branch unprotected.
- Inventory the existing rule. Read the current settings with
gh api repos/OWNER/REPO/branches/main/protection. - Write an equivalent ruleset and create it with
enforcementset toevaluate. - Compare. Review the rule insights to see what the ruleset would have blocked, and check
gh api repos/OWNER/REPO/rules/branches/mainto confirm which rules apply. - Change the ruleset to
active. - Delete the classic rule once the ruleset covers everything it did.
Because the strictest result wins, running both together during step 4 is safe. It only duplicates the checks.
gh api repos/OWNER/REPO/branches/main/protection > old-protection.json
gh api --method POST repos/OWNER/REPO/rulesets --input ruleset.json
gh api repos/OWNER/REPO/rules/branches/mainSettings that do not map one to one
Check these before you delete the old rule:
enforce_admins: truehas no single switch in a ruleset. Instead, leavebypass_actorsempty so that nobody is exempt.require_code_owner_reviewsbecomesrequire_code_owner_reviewinside thepull_requestrule.strict: truebecomesstrict_required_status_checks_policy.- A ruleset has no switch for blocking force pushes. The
non_fast_forwardrule does that job, anddeletionblocks deletes.
Exercise
Your team has a classic rule on main that requires one approval and a passing build. You want to move to a ruleset without a gap in protection. In what order do you work, and what do you check before deleting the classic rule?
Show answer
Create the ruleset in evaluate mode, review what it would have blocked and confirm the applied rules with the rules/branches endpoint, switch it to active, then delete the classic rule. Before deleting, confirm the ruleset is active and includes the pull request rule (one approval) and the required build check, with strict checks and an empty bypass list if administrators were previously enforced.