C.W.K.
Stream
Lesson 05 of 05 · published

Protected Branches and Review Gates

~18 min · protected-branches, review

Level 0Untracked Rookie
0 XP0/47 lessons0/14 achievements
0/100 XP to next level100 XP to go0% complete

Protected branches turn policy into enforcement

The reason production-grade repos do not get accidentally broken is not that everyone is careful. It is that the platform refuses unsafe operations on important branches. Protected branches in GitHub, GitLab, and Bitbucket let you encode rules — required PR review, required CI passes, force-push prohibition, signed-commit requirement — at the branch level. Once configured, even a maintainer cannot force-push to main by accident.

The default protection set worth applying to every main: require a pull request before merging, require at least one approval, require status checks (your CI suite) to pass, dismiss stale approvals when new commits land, prohibit force-push, prohibit deletion. For larger teams, add: require CODEOWNERS review for sensitive paths, require linear history (forces rebase or squash merges), require signed commits.

CODEOWNERS is the file that maps paths to required reviewers. frontend/auth/* @auth-team means every PR touching auth code must be reviewed by the auth team. Combine this with branch protection's "Require review from Code Owners" and the rule is enforced — no one can merge auth changes without auth team sign-off, regardless of who is doing the merge.

Status checks are the other half of the gate. Required CI workflows — tests, type checking, linting, security scans — must report green before merge is allowed. This is what prevents the classic "I just pushed; tests will pass eventually" mistake. Combined with auto-merge ("merge when checks pass"), the developer creates a PR with auto-merge enabled and walks away; the platform integrates only when every gate is green.

Code

Recommended GitHub branch protection (web)·text
Settings → Branches → Add rule for `main`:

[x] Require a pull request before merging
    [x] Require approvals (1+)
    [x] Dismiss stale pull request approvals when new commits are pushed
    [x] Require review from Code Owners
[x] Require status checks to pass before merging
    [x] Require branches to be up to date before merging
    Required checks: ci/test, ci/lint, ci/typecheck
[x] Require conversation resolution before merging
[x] Require signed commits
[x] Require linear history
[x] Restrict who can push to matching branches (admins only)
[x] Do not allow force pushes
[x] Do not allow deletions
CODEOWNERS file at repo root·text
# .github/CODEOWNERS
# Order matters — last matching pattern wins.

# Default reviewer for everything
*       @core-maintainer

# Auth code requires the auth team
/backend/auth/         @auth-team
/frontend/src/auth/    @auth-team

# Infra changes require platform team
/infra/                @platform-team
*.tf                   @platform-team

# Docs only require the docs team
/docs/                 @docs-team

External links

Exercise

On any repo where you have admin rights, configure branch protection on main with at least: required PR, one approval, required status checks, no force-push. Add a minimal .github/CODEOWNERS file with one path-to-team mapping. Try to force-push to main from your terminal and read the rejection. Try to merge a PR before the required check finishes and observe what GitHub allows. Note in writing one rule you would add for a larger team.

Progress

Progress is local-only — sign in to sync across devices.
Spotted a bug or have feedback on this page?Report an Issue

Comments 0

🔔 Reply notifications (sign in)
Sign inPlease sign in to comment.

No comments yet — be the first.