Sub topic 9
Required approving reviews without CODEOWNERS
This chapter assumes you can create branches and pull requests, and that you know repository roles (read, write, maintain, admin). It covers the review requirement that works without a CODEOWNERS file. The next chapters add code owners on top of it.
What a required approving review does
A branch protection rule can require a number of approving reviews before a pull request merges into the protected branch. Any reviewer with write access or higher counts. GitHub does not check which files they know or own.
This gives you a basic "four eyes" rule. It does not guarantee that the right person looked at the right files. That gap is what CODEOWNERS fills.
The settings that shape the rule
These options live under the branch protection rule's "Require a pull request before merging" section.
| Setting | Effect |
|---|---|
| Required number of approvals | Number of approving reviews needed from people with write access. |
| Dismiss stale pull request approvals when new commits are pushed | A new push removes existing approvals, so reviewers must look again. |
| Require approval of the most recent reviewable push | Someone other than the person who pushed last must approve. |
| Require review from Code Owners | Adds the code owner requirement. Covered later. |
Without the dismissal option, a contributor can get an approval and then push unreviewed changes. The pull request stays approved. Turn dismissal on for any branch where review is a real control.
The last-push option closes another gap. Without it, you could approve your own pull request after pushing a co-author's commits, if another reviewer had already approved earlier.
Setting it with the API
The same settings are available through the REST API. This example requires two approvals and dismisses stale ones:
gh api -X PUT repos/OWNER/REPO/branches/main/protection \
--input - <<'EOF'
{
"required_status_checks": null,
"enforce_admins": true,
"required_pull_request_reviews": {
"required_approving_review_count": 2,
"dismiss_stale_reviews": true,
"require_last_push_approval": true
},
"restrictions": null
}
EOFReplace OWNER and REPO with real values. This call replaces the whole protection configuration for the branch, so include every setting you want to keep.
How the review flow looks
Limits of this approach
- Any qualifying reviewer satisfies the count, even if they have never touched the code under change.
- The pull request author cannot approve their own pull request toward the count.
- A review that requests changes can block the merge until it is dismissed or replaced by an approval from the same reviewer.
- Reviews from people without write access appear on the pull request but do not count toward the requirement.
Exercise
A contributor gets one approval on a pull request, then pushes a new commit that changes the logic. The branch requires one approval. Which setting decides whether the earlier approval still counts, and what does turning it on do?
Show answer
"Dismiss stale pull request approvals when new commits are pushed" decides it. When on, the new push removes the existing approval, so a reviewer must approve the updated code before the pull request can merge.