Sub topic 8
Which Branch's CODEOWNERS Applies to a Pull Request
This chapter assumes you can open pull requests, you know what a base branch is, and you have read the earlier chapters on CODEOWNERS locations, patterns, and automatic review requests.
The rule: the base branch decides
GitHub reads the CODEOWNERS file from the pull request's base branch. The base branch is the branch the pull request will merge into, such as main. GitHub does not read the file from the head branch, which is the branch that holds your changes.
This has three practical effects:
- Editing CODEOWNERS on a feature branch does not change who is asked to review that same pull request.
- A new rule takes effect for pull requests only after the change merges into the base branch.
- Pull requests that target different base branches can have different owners for the same file.
Worked example
Suppose main contains this rule:
# .github/CODEOWNERS on main
/apps/web/ @example-org/web-teamA developer opens a branch called reassign-web and changes the rule:
# .github/CODEOWNERS on reassign-web
/apps/web/ @example-org/new-web-teamThe same branch also edits apps/web/index.js. When the developer opens a pull request into main, GitHub still requests @example-org/web-team, because that is what main says. The new-web-team group is not asked until the CODEOWNERS change is merged.
This is a safety property. If the head branch's file were trusted, an author could remove the rule that requires review, and then merge their own change without it.
Pull requests to other branches
Each base branch uses its own copy of the file. A pull request from feature-a into release/1.0 uses the CODEOWNERS file on release/1.0. If that branch has no CODEOWNERS file in a supported location, no owners are requested for that pull request, even if main has a complete file.
Common errors
- Backporting a rule: You add an owner to
main, but pull requests intorelease/1.0still skip it. Merge or cherry-pick the CODEOWNERS change into each long-lived branch that needs it. - Testing a new rule in its own pull request: The pull request that adds the rule does not use it. Merge the change first, then open a second pull request that touches the owned paths to confirm it works.
- Retargeting a pull request: If you change the base branch, GitHub evaluates the file from the new base. The requested reviewers can change as a result.
<br>
Exercise
A pull request from topic into main edits src/api/handler.py and also adds the line /src/api/ @example-org/api-team to CODEOWNERS on topic. main has no rule for /src/api/. Is api-team requested for review on this pull request? Why or why not?
Show answer
No. GitHub reads CODEOWNERS from the base branch, main, and main has no rule for /src/api/. The line on topic has no effect until it is merged into main.