Sub topic 1
What repository rulesets are and why they exist
A repository ruleset is a named set of rules that GitHub enforces on branches, tags or pushes. You can define one ruleset and apply it to a single repository or to many repositories at once. This chapter explains what a ruleset contains, what problems it solves, and how it differs from the older branch protection rules. Later chapters cover each part in detail.
Prerequisites
This chapter assumes you already know the following:
- Basic Git branching and pull requests: what a default branch is, and what a force push does.
- GitHub organisations, teams and repository roles such as Write and Admin.
- GitHub Actions workflows and status checks, at the level of knowing that a workflow run reports a pass or fail result on a commit.
- How to read JSON, because rulesets are stored and exchanged as JSON.
You do not need admin rights to follow along. You need them only if you want to try the examples on a real organisation.
The problem rulesets solve
Imagine an organisation with 200 repositories. Policy says that nobody may delete or force-push to the default branch, and that every change needs one approving review. Classic branch protection rules can express this policy, but only one repository at a time. Each repository admin has to create the rule, and each admin can later edit or remove it.
This causes three problems:
- Drift. Repositories created next month start with no protection until someone remembers to add it.
- Inconsistency. Different repositories end up with slightly different settings for the same policy.
- No separation of duties. Anyone with admin rights on a repository can weaken its protection, even if the policy came from the security team.
Rulesets address these problems. An organisation owner can write the policy once, select repositories by name pattern, and let GitHub apply it to matching repositories, including ones created later. Repository admins cannot edit an organisation ruleset from inside their repository.
Anatomy of a ruleset
Every ruleset has the same four parts, plus a name and a target type.
| Part | What it answers | Example |
|---|---|---|
| Target | Which kind of thing is governed: branches, tags or pushes | `branch` |
| Conditions | Which refs, and for organisation rulesets which repositories, are in scope | The default branch of every repository except `sandbox-*` |
| Rules | What is restricted or required | Block deletion, require one approving review |
| Bypass actors | Who may skip the rules, and how | Repository admins, only through a pull request |
| Enforcement status | Whether the rules are applied | `active`, `evaluate` or `disabled` |
A ref is a Git name that points to a commit, such as a branch (refs/heads/main) or a tag (refs/tags/v1.0.0). Conditions match refs by name, using patterns like ~DEFAULT_BRANCH, ~ALL, or fnmatch-style globs such as release/*.
The diagram simplifies two details. Rules from several rulesets are combined rather than evaluated in isolation, which the chapter on overlapping rulesets covers. Bypass actors can also be limited to pull requests only.
Three kinds of target
The target decides which rules you can choose from.
- Branch rulesets govern branch names. Typical rules: block deletion, block force pushes, require a pull request, require status checks, require linear history, require signed commits.
- Tag rulesets govern tag names. They are mostly used to stop release tags from being moved or deleted.
- Push rulesets govern the content of a push rather than the ref name. They restrict file paths, file sizes and file extensions. They apply to every branch in the repository, not to a name pattern.
A single ruleset has one target type. If you want both a branch policy and a tag policy, you create two rulesets.
Repository rulesets and organisation rulesets
Rulesets exist at two scopes.
- A repository ruleset lives in one repository and is managed by people with admin access to it.
- An organisation ruleset lives at the organisation level. It adds a repository condition, so you can include or exclude repositories by name, and it is managed by organisation owners or users with a custom role that allows it.
The rules themselves are the same at both scopes. What changes is who controls the ruleset and how far it reaches. Organisation rulesets are the tool for enforcing policy across many repositories, which is the subject of this tutorial. Availability of some features depends on your GitHub plan, for example whether organisation rulesets can cover private repositories, and the evaluate status needs GitHub Enterprise. Check the current GitHub documentation for your plan.
Rulesets compared with classic branch protection
Branch protection rules came first and still work. Rulesets add capabilities that branch protection never had.
| Aspect | Classic branch protection | Rulesets |
|---|---|---|
| Scope | One repository | One repository or many, at organisation level |
| Targets | Branches only | Branches, tags and pushes |
| Overlap | Rules for the same branch can conflict or one wins | Multiple rulesets can apply, and the most restrictive result applies |
| Bypass | Admins bypass by default unless you opt out; per-user exceptions only on some settings | Explicit bypass list of roles, teams, apps; can be limited to pull requests |
| Testing | None | `evaluate` status shows what would fail |
| Visibility | Mostly visible to admins | Anyone with read access can see the active rules |
| Required workflows | Not available | Available as a rule type |
The "most restrictive result applies" row matters in practice. With branch protection, you could not easily layer an organisation policy under a repository's own settings. With rulesets you can: the organisation sets a baseline, and a repository adds stricter rules on top. A repository cannot loosen the baseline.
A minimal ruleset
Here is the smallest useful ruleset body for a repository. It targets the default branch and blocks deletion and force pushes. It uses evaluate so nothing is blocked yet.
{
"name": "protect-default-branch-minimal",
"target": "branch",
"enforcement": "evaluate",
"conditions": {
"ref_name": {
"include": ["~DEFAULT_BRANCH"],
"exclude": []
}
},
"rules": [
{ "type": "deletion" },
{ "type": "non_fast_forward" }
]
}The rule non_fast_forward is how the API names "block force pushes". A force push rewrites history, so the new tip is not a fast-forward of the old one. Note there is no bypass_actors key, so nobody can bypass. The full body in a later chapter adds a repository_name condition, which only organisation rulesets accept.
You can send this body to a repository with the GitHub CLI. Replace OWNER/REPO with your own repository, and note that evaluate mode may be rejected on plans that do not support it. In that case use "active" on a test repository.
gh api \
--method POST \
-H "Accept: application/vnd.github+json" \
/repos/OWNER/REPO/rulesets \
--input protect-default-branch-minimal.jsonCommon errors and misunderstandings
- Assuming a ruleset replaces branch protection automatically. Both systems can apply to the same branch at once, and the stricter result wins. Existing branch protection rules stay in place until you remove them.
- Forgetting that a ruleset targets one type. A branch ruleset does nothing for tags. Pattern
refs/tags/*in a branch ruleset matches no branches. - Leaving enforcement on
disabled. A ruleset in this state looks configured but enforces nothing. Check the status first when a rule seems to be ignored. - Creating a ruleset with an empty bypass list and then needing an emergency fix. Decide who may bypass before you activate the ruleset. Chapter on bypass actors covers narrowing this.
- Expecting a repository admin to override an organisation ruleset. They cannot, unless the ruleset's bypass list includes their role.
Where this chapter fits
This chapter established the vocabulary: target, conditions, rules, bypass actors and enforcement. The next chapters take one part at a time, starting with the two scopes, then name patterns for refs, then repository selection, then the rules themselves. By the end of the tutorial you will build an organisation ruleset that requires a shared workflow to pass before merging.
Exercise
A repository admin deletes the branch protection rule on main in one repository, and the organisation also has an active organisation ruleset that blocks force pushes to default branches. Can someone now force-push to main in that repository? Explain why.
Show answer
No, unless they are on the ruleset's bypass list. The organisation ruleset is separate from the branch protection rule, and a repository admin cannot edit or delete it from inside the repository. Deleting the branch protection rule only removes that rule's restrictions, so the ruleset's non_fast_forward rule still applies.
Exercise
You want to stop anyone moving or deleting tags such as v1.2.0, and also reject any push that contains a file larger than 50 MB. How many rulesets do you need, and what target type does each have?
Show answer
Two rulesets. One has target tag to restrict deletion and updates of tags. The other has target push to restrict file size. A ruleset has a single target type, so branch, tag and push policies cannot share one ruleset.
Exercise
Why would you create a new ruleset with enforcement evaluate rather than active?
Show answer
In evaluate mode GitHub does not block anything, but records which actions would have failed. You can review those results in rule insights, fix mistakes in the rules or bypass list, and then switch to active without disrupting developers.