Skip to content
Visual explainer 1h 30m to learn 3 steps

Branch protection, rulesets & CODEOWNERS

Require reviews and passing checks before merge.

By Invicly Tech, , in Git, GitHub & GitHub Actions. About 10 minutes to read.

Topic 7 of 18

In short

Branch protection and rulesets stop anyone from pushing straight to main. They require reviews and passing checks, and CODEOWNERS automatically requests review from the right people.

Explained simply

Without protection, anyone with write access can push straight to main, skip review and break the build for everybody. Branch protection is a set of rules GitHub enforces on a branch, such as "changes must come through a reviewed pull request" and "tests must pass first".

Rulesets are the newer, more flexible way to write those rules. One ruleset can cover many branches by pattern, like main and every release/ branch. Several rulesets can apply at once, and each has a clear list of who, if anyone, may bypass it.

Status checks are the green ticks and red crosses that CI posts on a pull request. When you mark a check as required, the merge button stays disabled until that check passes. This is how "main is always green" becomes a rule instead of a hope.

CODEOWNERS is a file that says who owns which part of the code. When a PR touches those files, GitHub asks the owners to review automatically. Combined with the rule "require review from code owners", the platform team must approve changes to infrastructure, and so on.

Press Play on a diagram to walk through it one step at a time.

SequenceMerging into protected main
Developer
GitHub
CI
Code owner
  1. 1Open PR to main
    Developer to GitHub: Open PR to main
  2. 2Trigger required checks
    GitHub to CI: Trigger required checks
  3. 3Request review via CODEOWNERS
    GitHub to Code owner: Request review via CODEOWNERS
  4. 4Checks passed
    CI to GitHub: Checks passed
  5. 5Approve
    Code owner to GitHub: Approve
  6. 6Merge button enabled
    GitHub to Developer: Merge button enabled
FlowWhat the merge button waits for
  1. 1
    PR opened

    CODEOWNERS requests owner reviews

  2. 2
    Required checks

    Each named check must report success

  3. 3
    Approvals

    Enough approvals, including code owners

  4. 4
    Conversations

    All review threads resolved

  5. 5
    Up to date

    Optional: branch includes latest main

  6. 6
    Merge enabled

    Or the PR joins the merge queue

In depth

The rules worth turning on

Require a pull request before merging, with at least one approval. Turn on dismiss stale approvals when new commits are pushed, so an approval covers the code that actually merges. Require approval of the most recent reviewable push, so the last push must be approved by someone other than the person who pushed it. Require conversation resolution so no review thread is ignored.

Require status checks to pass, and pick the exact CI jobs. Block force pushes and deletions on main. Optional extras: require linear history (only squash or rebase merges), require signed commits, and require a merge queue for busy repos. Finally, decide on bypass: by default administrators may be exempt, so remove that unless you have a documented break-glass reason.

  • Required approvals: 1 for most teams, 2 for critical repos
  • Dismiss stale approvals on new pushes
  • Block force pushes and branch deletion
  • Free plan: protection works on public repos; private repos need a paid plan

Rulesets vs classic branch protection

Classic branch protection is one rule per branch pattern, and only one rule applies to a given branch. Rulesets are named, can target branches or tags by patterns such as ~DEFAULT_BRANCH or release/*, and can stack: when several rulesets apply to the same branch, all of their rules apply and the most restrictive setting wins. They can also be defined once at organization level for many repos.

Each ruleset has an enforcement status (Active or Disabled, plus Evaluate on Enterprise to test without blocking) and an explicit bypass list of roles, teams or apps. Anyone with read access can see which rulesets apply, which makes "why can I not merge?" easy to answer. Rulesets and classic rules can coexist; both are enforced.

CompareClassic protection vs rulesets
Classic branch protection
  • One rule per branch pattern
  • Branches only
  • Per repository
  • Admins exempt unless told otherwise
Rulesets
  • Many rulesets stack, strictest wins
  • Branches, tags and pushes
  • Repo or organization level
  • Explicit bypass list, visible to readers

Required checks: names and traps

A required check is matched by its name, which for GitHub Actions is the job name, not the workflow name. A matrix job reports one check per combination, like test (ubuntu-latest, 3.12). Renaming a job or changing a matrix silently creates new check names, and the old required name then waits forever. A common fix is a small final job that needs all the others and is the only required check.

Two more traps. If a whole workflow is skipped because of a paths or branches filter, its checks stay Pending and block the PR. A job skipped by an if condition, however, reports success. And require branches to be up to date makes every PR rebase or merge main before merging, which is safe but slow on busy repos; a merge queue solves that.

How CODEOWNERS is matched

GitHub looks for CODEOWNERS in .github/, then the repo root, then docs/, and uses the first one it finds. The file on the PR's base branch is the one that counts. Each line is a pattern followed by one or more owners: @user, @org/team or an email. Patterns work mostly like .gitignore, but ! negation and [ ] ranges are not supported.

The last matching line wins, and only its owners apply. So put the catch-all * line first and specific paths below. An owner must have write access to the repo, and a team must be visible and have write access, or the line is ignored. Without the rule require review from code owners, CODEOWNERS only auto-requests reviews; it does not block anything.

  • A line with a pattern and no owner removes ownership for those paths
  • /docs/ matches the folder at the root; docs/ matches any docs folder
  • GitHub shows errors in CODEOWNERS when you view the file on the web

Learn it in 3 short steps

About 30m each. Track them with a timer in your plan.

  1. 1
    Protect main with required reviews~30m
  2. 2
    Require passing status checks~30m
  3. 3
    Add a CODEOWNERS file~30m

Key points

  • Protect main so changes land only through reviewed pull requests.
  • Mark CI jobs as required status checks so failing code cannot merge.
  • Rulesets can target many branches and stack, with explicit bypass lists.
  • In CODEOWNERS the last matching pattern wins, so put broad rules first.

Key terms

Tap a card to flip it. 0/4 known

Examples

text
# .github/CODEOWNERS
*               @acme/backend
/infra/         @acme/platform
/.github/       @acme/platform
/docs/          @acme/docs @alice

Default owners first, specific paths after.

One stable required check for a matrix

Your CI has a test matrix of 6 combinations that changes often, and updating required checks every time is painful. Step 1: add a final job named ci-ok that needs every other job and runs even if they fail. Step 2: make it fail if any needed job did not succeed. Step 3: mark only ci-ok as required in the ruleset. Now you can change the matrix freely and the rule stays correct.

yaml
  ci-ok:
    if: always()
    needs: [lint, test]
    runs-on: ubuntu-latest
    steps:
      - name: Fail if any needed job failed or was cancelled
        if: contains(needs.*.result, 'failure') || contains(needs.*.result, 'cancelled')
        run: exit 1
      - run: echo "All required jobs passed"

CODEOWNERS for a monorepo

A repo holds the API, the web app and Terraform. Step 1: set a default owner team on the first line. Step 2: add specific paths below it, because the last match wins. Step 3: make the platform team own .github/ itself, so nobody can quietly change CI or remove owners. Step 4: turn on require review from code owners in the ruleset.

text
# .github/CODEOWNERS (last match wins)
*                 @acme/maintainers
/api/             @acme/backend
/web/             @acme/frontend
/infra/           @acme/platform
*.tf              @acme/platform
/.github/         @acme/platform
/docs/            @acme/docs @alice

Quick reference

  • Required check names are job names; a matrix makes one check per combination
  • A workflow skipped by paths filters leaves required checks Pending
  • A job skipped by an if condition reports success
  • Turn on dismiss stale approvals so approvals match the merged code
  • CODEOWNERS: .github/, then root, then docs/; first file found is used
  • In CODEOWNERS the last matching pattern wins, so put * first
  • Rulesets stack: if several apply, the most restrictive rule wins
  • gh api repos/OWNER/REPO/rulesets lists rulesets from the terminal

Common mistakes

  • Requiring a check by a job name that never runs, so every PR waits forever.
  • Letting admins bypass rules by default, which quietly defeats them.

Quick quiz

Pick an answer to see if you're right and why.

Question 1

1You require the check test, then rename the job to unit-tests. What happens to new PRs?

Question 2

2Two CODEOWNERS lines match infra/main.tf: * @acme/maintainers then *.tf @acme/platform. Who owns it?

Question 3

3Two rulesets apply to main: one needs 1 approval, the other 2. How many are required?

Question 4

4What does dismiss stale pull request approvals do?

0/4 answered

Practice exercises

Try each one yourself first. Use the hint if you get stuck, then compare with the solution.

  1. Exercise 1easy

    Add a CODEOWNERS file that makes you the default owner and a teammate the owner of docs/. Open a PR touching docs/ and check who is requested.

  2. Exercise 2medium

    Create a ruleset on the default branch that requires a pull request with 1 approval, blocks force pushes and deletion, and requires the check ci-ok.

  3. Exercise 3hard

    A docs workflow has a paths filter so it only runs when files in docs/ change, and its check is required. Code-only PRs are stuck on Pending. Fix it without removing the requirement.

Check yourself

Answer in your head, reveal, then be honest with yourself.

Feel confident about this topic?

Frequently asked questions

Should I use rulesets or classic branch protection?
Use rulesets for new setups. They stack, cover tags, can be shared across an organization and show their bypass list clearly. Classic rules still work and are enforced alongside them.
Why is my required check stuck on Expected or Waiting?
No job with that exact name reported on the latest commit. Usually the job was renamed, a matrix changed, or the workflow was skipped by a paths or branches filter.
Can repository admins bypass branch protection?
With classic protection, admins can bypass unless you enable the setting that includes administrators. With rulesets, only roles or teams on the bypass list can bypass.
Does CODEOWNERS require approval by itself?
No. It only requests reviews. You must also turn on require review from code owners in branch protection or a ruleset.

Go deeper

ShareXLinkedInWhatsApp