Sub topic 12
Branch protection and required reviews
This chapter assumes you can open a pull request, request reviewers, and read status checks on a pull request. Branch protection turns those features from conventions into rules that GitHub enforces.
What branch protection does
A branch protection rule applies to a branch name or pattern, such as main. Without a rule, anyone with write access can push straight to main or merge a pull request whose checks have failed. With a rule, GitHub refuses to merge until the conditions you set are met.
You configure rules under Settings > Branches in a repository. GitHub also offers rulesets, which cover the same ground and can apply to many branches or repositories at once. The options below appear in both.
The rules that matter most
| Setting | Effect |
|---|---|
| Require a pull request before merging | Blocks direct pushes to the branch. |
| Required approvals | Sets how many approving reviews are needed. |
| Dismiss stale approvals | Clears approvals when new commits are pushed. |
| Require review from Code Owners | Needs approval from the owners listed in `CODEOWNERS` for the changed paths. |
| Require status checks to pass | Blocks merging until named checks succeed. |
| Require conversation resolution | Blocks merging while review threads are unresolved. |
| Do not allow bypassing | Applies the rules to administrators too. |
How the gates combine
A pull request must clear every enabled gate. A request-changes review also blocks the merge until the reviewer approves or the review is dismissed.
Connecting checks to the rule
A required status check is matched by name. The workflow below produces a check named test, which you then select in the rule.
name: CI
on:
pull_request:
branches: [main]
jobs:
test:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4
- run: npm ci
- run: npm testA check can only be selected after it has run at least once in the repository. If you rename the job, the old name stays required and no job reports it, so the pull request waits forever.
For code owners, a CODEOWNERS file such as this one makes GitHub request review from @my-org/docs-team whenever files under /docs/ change:
# .github/CODEOWNERS
*.js @octocat
/docs/ @my-org/docs-teamCommon errors
- Waiting on a check that never reports. The check name in the rule does not match any job name, or the workflow's
on:filter skips that pull request. - Approvals vanish after a push. Dismiss stale approvals is on. This is intended. Ask the reviewer to look at the new commits.
- You cannot approve your own pull request. Authors never count toward required approvals.
- Owners are never requested. Code owner review is on, but the owner lacks write access or the pattern in
CODEOWNERSdoes not match the path. Later matching lines override earlier ones. - Admins merge around the rules. Unless you disable bypassing, administrators can override a rule.
Exercise
A pull request has two approvals and passing checks, but the merge button is disabled. The rule requires code owner review, and the PR changes /docs/setup.md. Name two likely causes.
Show answer
The @my-org/docs-team owners have not approved, since approvals from other reviewers do not satisfy code owner review. Or an approval was dismissed as stale after a new push, so the owners' approval no longer counts. Open conversations would also block the merge if conversation resolution is required.