Skip to content
Tutorials Center
My learning

Topics

Lesson 14 of 36 ยท Pull Requests and Reviews

Branch Protection, Rulesets and CODEOWNERS

Protect your main branch: require reviews and status checks, block force pushes, use rulesets, and assign automatic reviewers with a CODEOWNERS file.

A pull request workflow only works if people cannot skip it. If anyone with Write access can push straight to main, a rushed commit can break production with no review and no tests. Branch protection rules and the newer rulesets let you enforce the process that your team agreed on, so it holds even when someone is in a hurry.

Branch protection rules

Go to Settings > Branches > Add branch protection rule, enter a branch name pattern (main, release/*), and tick the protections you need:

Setting What it enforces
Require a pull request before merging No direct pushes; changes arrive through a PR
Required approvals (1, 2...) Minimum number of approving reviews
Dismiss stale approvals New commits invalidate earlier approvals
Require review from Code Owners The owners listed in CODEOWNERS must approve changes in their paths
Require status checks to pass Selected CI checks must be green, optionally up to date with the base
Require conversation resolution All review threads resolved
Require signed commits Every commit must carry a verified signature
Require linear history Forbids merge commits
Require merge queue PRs merge through the queue
Do not allow bypassing the above settings Applies to administrators too
Restrict who can push Only chosen people or teams can push
Allow force pushes / Allow deletions Off by default, keep them off for important branches

A sensible baseline for a team project: require a PR, one approval, passing status checks, resolved conversations, and no force pushes.

By default, repository admins can bypass protection. Tick Do not allow bypassing (or use rulesets with an explicit bypass list) if you want the rules to bind everyone, including you at 2 a.m.

Rulesets

Rulesets (Settings > Rules > Rulesets) are the modern replacement that fixes several limits of classic rules:

  • Several rulesets can apply to the same branch and layer; the strictest result wins.
  • They can target branches, tags and push content, and with patterns such as refs/heads/release/**.
  • They have an enforcement status: Active, Evaluate (log what would have been blocked without blocking, ideal for trying new rules) or Disabled.
  • An explicit bypass list names roles, teams or apps that may skip the rules, and every bypass is auditable.
  • Organization owners can create rulesets that apply across many repositories.
  • Extra rules include restricting file paths, file size and commit message patterns, requiring deployments to succeed, and blocking creation or deletion of refs.

Start with Evaluate, read the insights to see what the rule would have caught, then switch to Active.

JS Stand-in: does this push or merge pass the rules?

Press Run (or Ctrl + Enter) to see the console output here.

No output.

Status checks

A status check is a pass or fail signal from a CI workflow or an external service attached to a commit. After a check has run at least once in the repository, you can select it as required in the rule. A check that never reports leaves the PR waiting forever, so make sure the workflow runs on every PR (and not only on certain paths) if the check is required. Workflows are covered in the Actions chapter.

CODEOWNERS

A CODEOWNERS file tells GitHub who is responsible for which paths. It lives at .github/CODEOWNERS, docs/CODEOWNERS or the repository root. The syntax resembles .gitignore, with owners listed after each pattern:

TEXT
# Default owners for everything
*                     @octo-org/maintainers

# Frontend code
/web/                 @octo-org/frontend
*.css                 @octo-org/frontend @grace

# Infrastructure and CI need an ops review
/.github/workflows/   @octo-org/ops
/terraform/           @octo-org/ops

# Documentation: no owner, so anyone can change it
/docs/
  • The last matching pattern wins (opposite of what some expect), so put general rules first and specific ones after.
  • Owners can be users, teams (@org/team, who must have Write access) or emails.
  • A pattern with no owners removes ownership for that path.
  • When a PR touches owned files, GitHub automatically requests review from the owners. With Require review from Code Owners enabled, their approval becomes mandatory.
JS Stand-in: who owns this file?

Press Run (or Ctrl + Enter) to see the console output here.

No output.

Putting it together

flowchart LR
  P["Open PR"] --> C["Required checks<br/>pass?"]
  C -- no --> X["Blocked"]
  C -- yes --> R["Approvals and code<br/>owner review?"]
  R -- no --> X
  R -- yes --> T["Conversations<br/>resolved?"]
  T -- no --> X
  T -- yes --> M["Merge allowed"]

Every gate you enable adds one more reason for the merge button to stay disabled.

Do not over-protect: if the rules are so heavy that nobody can ship a hotfix, people will find workarounds. Document an emergency path (a bypass role with an audit trail) instead of leaving admins to improvise.

Check your understanding

Quick check ยท 4 questions

of 4 answered You scored / 4 ยท perfect!

  1. 1. In a CODEOWNERS file with overlapping patterns, which rule applies to a file?

    The last matching pattern takes precedence, so list general patterns first.
  2. 2. What does the Evaluate enforcement status of a ruleset do?

    Evaluate mode lets you test a rule safely before activating it.
  3. 3. A required status check never reports on a PR. What happens?

    A required check that never runs blocks the merge; make sure the workflow triggers on every PR.
  4. 4. Which setting stops an earlier approval from counting after new commits are pushed?

    Stale approvals are dismissed so a reviewer sees the final code.

Summary

  • Protect important branches: require PRs, approvals, passing checks, resolved conversations and block force pushes.
  • Rulesets layer, target branches and tags, support Evaluate mode and audited bypass lists.
  • Required status checks must actually run on every PR.
  • CODEOWNERS maps paths to owners (last match wins) and can make their review mandatory.

Next: Issues: Tracking Work