# 4 GitHub settings that stop anyone breaking main

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

Source: https://fullbytedeveloper.com/blog/github-branch-protection-settings
Keyword: MAINGUARD
Published here: 2026-11-10, updated 2026-12-03

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](/blog/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.

<figure>
<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 720 280" role="img" aria-label="Four gates before main: no force, PR, approval, green checks">
  <defs>
    <marker id="mainGuardArrow" markerWidth="10" markerHeight="10" refX="8" refY="3" orient="auto">
      <path d="M0,0 L8,3 L0,6 Z" fill="#14b8a6"/>
    </marker>
  </defs>
  <rect x="0" y="0" width="720" height="280" rx="12" fill="#0b1f3a"/>
  <text x="24" y="36" fill="#f8fafc" font-family="ui-sans-serif, system-ui, sans-serif" font-size="16" font-weight="700">path to main</text>
  <rect x="24" y="70" width="130" height="64" rx="8" fill="#123052" stroke="#14b8a6" stroke-width="2"/>
  <text x="40" y="108" fill="#f8fafc" font-family="ui-sans-serif, system-ui, sans-serif" font-size="14">topic branch</text>
  <path d="M154 102 H190" stroke="#14b8a6" stroke-width="3" fill="none" marker-end="url(#mainGuardArrow)"/>
  <rect x="198" y="70" width="100" height="64" rx="8" fill="#123052" stroke="#f59e0b" stroke-width="2"/>
  <text x="214" y="108" fill="#f59e0b" font-family="ui-sans-serif, system-ui, sans-serif" font-size="14">PR only</text>
  <path d="M298 102 H334" stroke="#14b8a6" stroke-width="3" fill="none" marker-end="url(#mainGuardArrow)"/>
  <rect x="342" y="70" width="110" height="64" rx="8" fill="#123052" stroke="#f59e0b" stroke-width="2"/>
  <text x="358" y="108" fill="#f59e0b" font-family="ui-sans-serif, system-ui, sans-serif" font-size="14">1 approval</text>
  <path d="M452 102 H488" stroke="#14b8a6" stroke-width="3" fill="none" marker-end="url(#mainGuardArrow)"/>
  <rect x="496" y="70" width="100" height="64" rx="8" fill="#123052" stroke="#f59e0b" stroke-width="2"/>
  <text x="512" y="108" fill="#f59e0b" font-family="ui-sans-serif, system-ui, sans-serif" font-size="14">CI green</text>
  <path d="M596 102 H632" stroke="#14b8a6" stroke-width="3" fill="none" marker-end="url(#mainGuardArrow)"/>
  <rect x="640" y="70" width="56" height="64" rx="8" fill="#123052" stroke="#14b8a6" stroke-width="2"/>
  <text x="650" y="108" fill="#14b8a6" font-family="ui-sans-serif, system-ui, sans-serif" font-size="14">main</text>
  <rect x="24" y="160" width="672" height="96" rx="8" fill="#123052" stroke="#64748b" stroke-width="2"/>
  <text x="40" y="196" fill="#f8fafc" font-family="ui-sans-serif, system-ui, sans-serif" font-size="14">Also on the ruleset: block force pushes and restrict deletions [2].</text>
  <text x="40" y="224" fill="#cbd5e1" font-family="ui-sans-serif, system-ui, sans-serif" font-size="14">Bypass lists are break-glass only; empty is safer than "admins exempt" by habit.</text>
</svg>
</figure>

*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](/blog/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](/blog/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](/blog/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](https://fullbytedeveloper.com/blog/topic/dev-tools).

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

Image: 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.
https://fullbytedeveloper.com/media/github-branch-protection-settings/github-branch-protection-settings-venice.webp
Every push to main has to pass the same gates: no direct force, a PR, a review, and green checks.

## Sources

1. [GitHub docs: creating rulesets for a repository](https://docs.github.com/en/repositories/configuring-branches-and-merges-in-your-repository/managing-rulesets/creating-rulesets-for-a-repository)
2. [GitHub docs: available rules for rulesets](https://docs.github.com/en/repositories/configuring-branches-and-merges-in-your-repository/managing-rulesets/available-rules-for-rulesets)
3. [GitHub changelog: automatically migrate branch protection rules to repository rulesets](https://github.blog/changelog/2026-08-11-automatically-migrate-branch-protection-rules-to-repository-rulesets/)
4. [GitHub docs: Managing the publication of repositories](https://docs.github.com/en/repositories/managing-your-repositorys-settings-and-features/managing-repository-settings/setting-repository-visibility)
