·Why protect your main branch?
On GitHub, the main branch is usually the source of truth — what gets deployed, what teammates build on top of, what a release is cut from. Without any protection, anyone with write access can push directly to it, force-push over its history, or even delete it outright. A single mistaken command on someone's laptop can undo weeks of work.
GitHub solves this with Branch Protection Rules, or their newer, more flexible replacement, Rulesets. Both let you lock down a branch so that changes can only land through a reviewed pull request — never a direct push, and never a delete.
Rulesets vs. classic branch protection: Rulesets are the modern approach — they support multiple branch patterns, layered rules, and finer-grained targeting. This guide uses Rulesets, but the same protections (restrict deletions, block force pushes, require a pull request) exist under the older Settings → Branches screen too, if you prefer that interface.
1Step-by-step: creating a ruleset
Open your repository's Settings
Go to your GitHub repository, then click the Settings tab at the top of the page.
Find Rulesets in the sidebar
In the left sidebar, under Code and automation, click Rules → Rulesets. (If you'd rather use the classic interface, go to Branches instead.)
Create a new ruleset
Click New ruleset → New branch ruleset to start configuring your rule.
Name it and set the target branch
Give it a clear Ruleset Name (e.g. Main Branch Protection), keep Enforcement status set to Active, then under Target branches click Add target → Include default branch (or type main directly).
Enable the core protection rules
Check the boxes below to block direct pushes and deletion — details in the checklist right after this step.
Save your ruleset
Scroll down and click Create (or Save changes) to apply the ruleset immediately.
The rules you actually need to check
- Restrict deletions — prevents anyone from deleting the
mainbranch entirely. - Block force pushes — stops history-rewriting pushes (
git push --force) that could overwrite or erase commits. - Require a pull request before merging — forces every change to go through a PR instead of a direct push. Optionally set Required approvals to
1(or more) so a teammate must review and approve before merge.
2Extra best practices worth enabling
Once the basics are in place, a few more rules make the workflow noticeably safer for a real team:
Require status checks to pass before merging
If you have CI set up (GitHub Actions, or any automated test/build pipeline), enable this so a pull request can't be merged until those checks are green. It keeps broken builds off main.
Require conversation resolution before merging
When reviewers leave comments on a pull request, this rule blocks merging until every conversation thread is marked resolved — so feedback doesn't get silently ignored.
Restrict who can push to matching branches
Limit direct interaction with the protected branch to specific people or teams (e.g. admins or leads), so everyone else is required to go through a pull request by default.
3How to verify it actually works
Test the setup from your local machine by trying to push a small change directly to main:
git checkout main
echo "test" >> README.md
git commit -am "test: verify branch protection"
git push origin main
If the ruleset is active, GitHub will reject the push and tell you a pull request is required. That's confirmation the rule is working — from here on, every change needs to go through a branch and a PR.
Don't forget to revert the local test commit (git reset --soft HEAD~1) before you move on, so it doesn't linger on your branch.
?FAQ
Does this also block me, the repository owner?
By default, rules apply to everyone, including admins, unless you explicitly enable an admin bypass for the ruleset. Keeping it strict — no bypass — is usually the safer default, since it means every change, including yours, gets reviewed.
Can I protect more than one branch?
Yes. Rulesets support branch name patterns (e.g. release/*), so you can add multiple targets or create separate rulesets for main, develop, and release branches.
What if I need to make an emergency fix directly?
Open a pull request even for urgent fixes — it can be merged within minutes once approved. If a true bypass is unavoidable, GitHub lets ruleset admins temporarily grant bypass access rather than disabling protection entirely.