You protect a branch on GitHub by creating a rule for it in the repository settings, which stops people from pushing straight to it, force pushing, deleting it, or merging work that has not been reviewed and tested. These GitHub branch protection rules are how teams keep main stable. You need admin permission on the repository to set them up.
GitHub now has two systems for this: classic branch protection rules and rulesets. Both can enforce the same core checks, and this guide explains the common settings and then shows how rulesets differ.
Without protection, anyone with write access can push to main, rewrite its history with git push --force, or delete it. One mistaken push can break a release and erase teammates' work. Protection moves every change through a pull request, where review and automated checks happen before code reaches the branch.
Typical candidates are main, master, develop and any release/* branch.
Follow these steps in the web interface:
main or release/*.The pattern uses fnmatch syntax, so release/* covers release/1.0 and release/2.0. A rule applies to every branch that matches the pattern.
| Setting | What it enforces |
|---|---|
| Require a pull request before merging | No direct pushes; changes arrive through pull requests |
| Required approvals | A set number of approving reviews before the merge button works |
| Dismiss stale approvals | New commits remove earlier approvals, so reviewers see the final code |
| Require review from Code Owners | The people named in a CODEOWNERS file must approve changes to their files |
| Require status checks to pass | Selected CI jobs (tests, lint, build) must be green |
| Require branches to be up to date | The branch must contain the latest target commits before merging |
| Require conversation resolution | All review comments must be resolved |
| Require signed commits | Every commit must carry a verified signature |
| Require linear history | Merge commits are blocked; only squash or rebase merges are allowed |
| Do not allow force pushes | Blocks history rewriting (this is the default) |
| Do not allow deletions | Blocks deleting the branch (this is the default) |
A sensible starting set for a small team is: require a pull request, one approval, dismiss stale approvals, require the CI check, and block force pushes and deletions.
A status check only appears in the list after it has run at least once in the repository in the last week, so push a pull request first and then choose the check by name. If you rename a workflow job later, the old required name never reports and every pull request waits forever. When a merge is stuck on "Expected, waiting for status", compare the required names with the real job names.
A workflow that is skipped by a path filter also never reports its check. If a check is required, make sure the workflow runs on every pull request, or use a job inside the workflow that always finishes.
By default, classic rules do not apply to repository admins unless you enable Do not allow bypassing the above settings (older name: Include administrators). Turn it on if you want the same rules for everyone, which is a good habit because admins also make mistakes.
You can also list people, teams or apps that may bypass specific protections, for example a release bot that must push version commits. Keep this list short and review it regularly.
Rulesets are the newer, more flexible way to protect branches and tags. Compared with classic rules, they let you:
You find them under Settings, then Rules, then Rulesets. The rule types are similar to the settings above: require pull requests, require status checks, block force pushes, require linear history and so on. New projects usually start with rulesets, while old repositories often still use classic rules, and both can exist together.
After saving, test it:
On a protected branch the push is rejected with a message like this:
That error confirms that direct pushes are blocked. Undo the test with git reset --hard HEAD~1 on your local copy.
Branch protection for private repositories needs a paid plan (GitHub Pro, Team or Enterprise). Public repositories can use it for free, so check the current plan table for details.
Only if the rule allows admins to bypass it. Enable the option to apply the rule to administrators if you want no exceptions.
Usually a required review is missing, the branch is behind the target, a conversation is unresolved, or the required check name does not match any running job.
A classic rule is a per-branch setting, while a ruleset is a newer, layered system that can also cover tags, run in evaluate mode and apply across an organisation.
Open Settings, then Branches (or Rules for rulesets), match your important branches by pattern, and require pull requests, approvals, passing checks and no force pushes. Apply the rules to admins too, and test with a direct push to confirm they work.