Branch protection and rulesets
Turn 'main must stay green' into a rule GitHub enforces: require pull requests, reviews and passing checks, and block force pushes and deletion.
Pull requestsCode review on GitHubMerge methods: squash, rebase and mergeForce pushing safelyTrunk-based development
Branch protection and rulesets
The player loads from youtube-nocookie.com when you press play. Nothing reaches YouTube before that.
Not started
What you'll learn
- A team agreement that main must stay releasable only lasts until the first rushed push, so GitHub lets you enforce it with rules on the branch.
- Typical rules are: require a pull request, require approving reviews, require status checks to pass, block force pushes and restrict deletions.
- Classic branch protection rules and newer rulesets overlap, and rulesets add layering, an enforcement toggle without deleting the rule, and a bypass list.
- A required status check only protects you if its name matches a check that actually runs, so test the rule by opening a pull request that should be blocked.
- Rules that admins can bypass are weaker than they look, so decide deliberately who is on the bypass list.
Rules beat agreements
By now the team has agreed on a workflow: nobody commits straight to main, every change gets a pull request, and the tests must pass. It works for a while. Then someone is late for a deadline and pushes a “tiny fix” directly. It breaks the build. Or a new contributor does not know the agreement exists.
The fix is to make the rule something the server enforces. GitHub can reject a push or a merge that breaks the rules, before it lands. This is the safety net that trunk-based development relies on, where everyone merges often into one branch, so that branch has to be protected.
What you can require
The vocabulary below comes from GitHub’s docs, which I read on 2026-11-23. The names differ slightly between the two systems, so I give both where I could confirm them.
- Require a pull request before merging. Nobody pushes straight to the branch. Sub-options in rulesets include the number of approving reviews, dismissing stale approvals when new commits arrive, requiring review from code owners (the next lesson), and requiring all comments to be resolved. In classic protection this is Require pull request reviews before merging.
- Require status checks to pass before merging. The named checks, usually your test workflow, must succeed. A related option, Require branches to be up to date before merging, makes the PR include the latest base commits first.
- Require linear history. Merge commits are blocked, so people must use squash or rebase. This ties back to merge methods.
- Block force pushes and Restrict deletions. History on the branch cannot be rewritten or the branch removed.
- Require signed commits, plus deployment, code scanning and secret-scanning requirements that appear in newer rulesets.
Classic protection rules versus rulesets
GitHub has two systems for this, and both are live. Branch protection rules are the older one: one rule per branch pattern, configured under the repository’s settings. Rulesets are newer. The docs list these differences:
- More than one ruleset can apply to the same branch at once. Their rules are combined, and where they disagree the most restrictive one wins. Only one protection rule applies at a time.
- A ruleset’s enforcement can be changed without deleting it. The docs name Active and Disabled. The creation page also mentions an Evaluate option in one place, for testing without affecting contributors. I could not confirm how it behaves from the docs, so check it in your own settings.
- Anyone with read access can see the active rulesets.
- Rulesets can control commit metadata, such as message patterns and author emails.
- You can add a bypass list of roles, teams or apps who may skip the rules.
The docs say the two layer: if a branch has both a ruleset and a protection rule, all applicable rules are enforced. For a new repository, start with rulesets.
The documented route to a new one is: Settings, then Rulesets under “Code, planning, and automation”, then New ruleset, then New branch ruleset. You name it, choose the Enforcement status (the default is Disabled), optionally add a bypass, choose targets with Add a target, tick the rules, and click Create. A typical first ruleset for the notes project targets the default branch and ticks: require a pull request with one approval, require status checks, block force pushes, restrict deletions.
I could not use any of this. I have no GitHub account where I wrote the lesson, so I could not create a ruleset or see the interface. I also could not confirm from the docs which plans include rulesets for private repositories. The “About rulesets” page I read mentions the GitHub Team and Enterprise plans in the context of organization-wide rules. Check the current plan table before relying on them in a private repository.
What I could test: the idea of a server-side gate
I can show what “rejected by the server” looks like with a local stand-in. A bare repository can run a pre-receive hook that refuses pushes to main:
#!/bin/sh
while read old new ref; do
[ "$ref" = refs/heads/main ] || continue
echo "changes must be made through a pull request ($ref)" >&2
exit 1
done
With that in the bare repository’s hooks/ directory, pushing a commit straight to main printed ! [remote rejected] main -> main (pre-receive hook declined), while pushing a topic branch worked. That is the same shape as GitHub’s behaviour: the check is on the server, so no local setting can skip it. It is only an analogy, though. GitHub’s real rules are not a hook you can read, the wording of its rejection message is different, and I did not see it.
Making a rule that works
- Required checks must match real checks. A status check rule names a check. If you name
testbut your workflow’s job is calledbuild, nothing ever reports astest, and depending on the setup the merge may block forever or may not be guarded at all. After creating the rule, open a throwaway PR that should fail and confirm the merge button is blocked. - Decide about admins. By default, classic protection does not apply to people with admin permission, and a Do not allow bypassing the above settings option extends the rule to them. In rulesets you control this with the bypass list. A rule your most powerful users can silently skip protects less than you think.
- Do not over-protect. Requiring two reviewers on a three-person team makes everyone wait. Match the rules to your risk.
- Protect the workflow files. A contributor who can change
.github/workflows/can change what your checks do. CODEOWNERS is the usual answer.
What the video covers
Mickey Gousset’s “Introduction to GitHub Actions - Part 6 - Repository Rulesets” is about 15 minutes and was published around 2025 (YouTube shows “1 year ago”). Its description picks up from the previous episode, where a workflow builds and tests on a pull request, and asks how to make that workflow pass before a merge is allowed. That is the same goal as this lesson, and it uses rulesets, not classic branch protection. It is part of a series, so it assumes the earlier episodes’ workflow, and I could not read a transcript or chapters, so I cannot say how it handles bypass lists or the other rules above. Mickey Gousset works at GitHub, and the video is on his own channel. English is judged from the title, description and channel. It replaces a small-channel pick from the first draft. Two older videos, one on classic branch rules and GitHub’s short announcement of repository rules, are linked under deeper.
Where this fits
Rules say what must happen before a merge. The next lesson says who must look at which files, and how to give contributors a template to start from: CODEOWNERS and templates.
Check yourself
Why enforce rules on the branch instead of just agreeing to open pull requests?
Agreements fail under deadline pressure and when someone is new. A rule makes the safe path the only path and records exceptions.
What is a required status check?
A named check, such as your test workflow's job, that must pass on the pull request before it can merge. If no check with that name reports, the requirement stays unmet.
What can rulesets do that a single branch protection rule cannot?
Several rulesets can apply to the same branch at once and their rules combine, enforcement can be switched without deleting, and they can include a bypass list and rules about commit metadata.
Why is blocking force pushes on main important?
A force push rewrites the branch's history for everyone. Blocking it means shared history cannot be replaced by accident or malice.
Go deeper
Notes
Notes are saved to your account.
Everything else on this page works without one — progress and bookmarks keep themselves in this browser and come with you when you sign in. Notes are the one thing worth not trusting to a browser you might clear.
Your notes could not be loaded.