Full Byte Developer.
MAINGUARD

4 GitHub settings that stop anyone breaking main

Four branch rules that block direct pushes, require reviews, and enforce passing checks.

Published here , updated · 4 sources

Anyone with push access can break main unless you set branch rules. GitHub calls the newer system rulesets; classic branch protection still works, but rulesets are what GitHub recommends going forward [1][3]. Turn on four protections: block force and deletes, require a pull request, require an approval, and require named status checks.

Quick start: open a branch ruleset for main

In the GitHub UI: Settings, then Rules, then Rulesets, then New ruleset, then Branch ruleset. Target pattern: main (or master if that is still your default) [1].

With the gh CLI on a repo you admin (shape from GitHub's ruleset docs; adjust names):

bash
gh api repos/:owner/:repo/rulesets --jq '.[].name' 2>/dev/null || echo 'list rulesets in Settings > Rules when API scope is missing'
gh --version | head -1

Captured on this host:

text
gh version 2.103.0 (2026-10-21)

Use the UI for the first ruleset if you have not used the Rulesets API before. The four toggles below matter more than the entry path [1][2].

1. Block direct force pushes and deletions

Under Branch protections, enable Restrict deletions and Block force pushes [2].

You should see both restrictions listed in the ruleset summary. This stops "I fixed main with --force" from erasing history other people already pulled. Pair with the rebase rule in Git rebase vs merge: rewrite private topic branches, not the shared default branch.

2. Require a pull request

Enable Require a pull request before merging [2]. Direct pushes that skip the PR flow are blocked for everyone who is not in a bypass list you intentionally configured.

You should see the PR requirement listed under the ruleset. Feature work then lands through reviewable diffs instead of silent tip moves.

3. Require at least one approval

Inside Require a pull request before merging, set required approvals to 1 or more. Optionally enable Dismiss stale approvals when new commits are pushed [2].

A PR to main now waits for at least one reviewer approval. Stale-approval dismissal stops "approved yesterday, force-pushed a rewrite today" from merging on an old rubber stamp.

4. Require status checks to pass

Enable Require status checks to pass. Add the exact job names your CI reports (for example build, test, check) [2]. Only jobs you list gate the merge; other checks can still run.

A PR now cannot merge until the listed checks go green. Wrong names silently fail to protect you, so copy the check name from a real PR after CI runs once.

path to main topic branch PR only 1 approval CI green main Also on the ruleset: block force pushes and restrict deletions [2]. Bypass lists are break-glass only; empty is safer than "admins exempt" by habit.

Four gates turn "I pushed to main" into a reviewed, checked pull request.

Convert classic branch protection if you still have it

If Settings, then Branches still shows classic rules, use Convert to ruleset when GitHub offers it [3]. Review the mapped toggles afterward; names differ slightly between classic UI and rulesets.

Re-enable after making a private repo public

GitHub disables push rulesets when a private repository becomes public [4]. After a visibility flip, return to Settings, then Rules, and turn the protections back on. The make private repo public checklist calls this out as a required follow-up.

Admin bypass and CODEOWNERS

Rulesets can grant bypass to users, teams, or apps [1][2]. Keep that list short. Prefer CODEOWNERS for required reviewers on sensitive paths instead of giving everyone bypass. Document who can bypass and why, or the next incident will include "someone with bypass pushed at 2 a.m."

Status check naming gotchas

Checks must match the name GitHub shows on the PR, which may differ from the job name: in YAML or from a matrix suffix. After the first green run, copy the exact string into the ruleset. Require branches to be up to date before merging when you want to block merges that raced ahead of main.

Depth: why four settings beat one soft rule

A team charter that says "please open PRs" fails the first time someone is in a hurry. Rulesets enforce the charter in the merge button. Force-push blocks protect archaeology. PR and approval protect review. Status checks protect the build you already paid to run [1][2].

When you also rewrite history with rebase, keep that rewrite on private topic branches. Protected main plus local rebase before PR is a common safe pairing; see Git rebase vs merge.

Required linear history and squash policies

Some teams enable rules that require linear history on main, which blocks merge commits and pushes people toward rebase or squash. That is a product choice, not a moral law. If you require linear history, document how topic branches should update (rebase onto main before merge) and keep force-push blocked on main itself [2]. Pair with Git rebase vs merge so the team shares vocabulary.

Rulesets at organization level

Organization rulesets can enforce the same four protections across many repositories [1]. Start with one repo ruleset, prove the check names and approval count, then promote the pattern. Org rules that reference missing check names create false confidence at scale.

Deployments and environment gates

Environment protection rules (required reviewers before a deploy job runs) complement branch rulesets. Branch rules protect what lands in main. Environment rules protect what rolls out. Use both when production credentials sit in environment secrets [1][2].

Verify from a throwaway branch

Create a short-lived branch, open a PR, and confirm the merge button stays blocked until approval and checks arrive. Attempt a direct push to main from a non-bypass account and confirm rejection. Rules that only exist in a wiki do not count.

bash
# after protections exist: expect failure without bypass
git checkout -b probe-main-guard
echo probe >> README.md
git add README.md && git commit -qm probe
# git push origin HEAD:main   # should be rejected by ruleset
git push -u origin probe-main-guard

Mistakes that leave main unprotected

Creating a ruleset with no target pattern that actually matches main.

Requiring status checks but listing job names that never appear on PRs, so nothing gates merges.

Leaving admin bypass on for every repository admin "temporarily" for months.

Forgetting to re-enable rulesets after changing a private repository to public [4].

Blocking force pushes on main while still allowing direct pushes without a PR.

Enabling required linear history without teaching the team how to rebase private topic branches safely.

When the four settings are enough

Small teams can start with one approval and one check job. Grow the required check list as CI splits. Organization rulesets can later apply the same pattern across many repos [1]. Until then, one repository ruleset on main with these four toggles stops the most common "who broke main" stories. Revisit the ruleset after the first public incident; tighten bypass lists while the pain is fresh.

Browse the Dev tools topic.

Send this to the friend who pushed straight to main and broke the release branch.

Teal double-headed arrow on a dark grid, threaded through three vertical rings with glowing rims. Hollow glowing cube in the upper right; two flat teal discs in the lower right foreground.
Every push to main has to pass the same gates: no direct force, a PR, a review, and green checks.

Keep this guide

By Full Byte Developer

Filed under Dev tools, Security

Questions about this guide

Should I use rulesets or classic branch protection?

Rulesets are GitHub's newer system. They support pattern matching across branches and can be managed at the organization level. Classic rules still work, but GitHub recommends migrating.

Can I require reviews from specific people?

Yes. Use required reviewers or code owners. Rulesets let you configure bypass permissions for specific users, teams, or apps.

Do rulesets survive making a private repo public?

GitHub disables push rulesets when a private repository becomes public. Re-enable them after the visibility change.

Sources

Next in this stack