Learning center

Which CODEOWNERS file governs a pull request that edits it?

A pull request uses the CODEOWNERS file on its base branch, so ownership changes in the pull request apply only to later ones. A new owner is not requested on the pull request that adds them, and an owner being removed is still requested if that pull request also changes their code. Land ownership changes on their own, before the code they govern.

Which version GitHub reads#

GitHub's documentation states the rule for review requests: "To trigger review requests, pull requests use the version of CODEOWNERS from the base branch of the pull request. The base branch is the branch that a pull request will modify if the pull request is merged." CODEOWNERS and forks

The same rule covers forks, other branches and stacks. For a fork, "If you create a pull request from a fork, and the base branch is in the upstream repository, then the pull request will use the CODEOWNERS file from that branch in the upstream repository," so a contributor cannot change who reviews their pull request by editing the file in their fork. For branches, "Each CODEOWNERS file assigns the code owners for a single branch in the repository." CODEOWNERS file location A backport to a long-lived release branch is therefore reviewed by whoever that branch's file names, which may be months out of date. In a stack of pull requests, CODEOWNERS is "Evaluated from the stack base," not from the branch directly below. Stacked pull requests: branch protections

The documentation describes this rule for review requests. When Require review from Code Owners is on, read the pull request's merge box to see which approvals it is still waiting for.

A fictional new service#

Suppose a fictional repository acme/shop has this .github/CODEOWNERS on main:

*              @acme/platform
/services/     @acme/backend
/.github/      @acme/repo-admins

A developer opens a pull request that creates services/billing/ and, in the same change, adds a line for its new team:

*                    @acme/platform
/services/           @acme/backend
/services/billing/   @acme/billing
/.github/            @acme/repo-admins

GitHub reads main's version. For the files under services/billing/, the last matching line on main is /services/ @acme/backend, so the backend team is requested and the billing team is not. The edit to .github/CODEOWNERS requests @acme/repo-admins. Once both teams approve, the pull request can merge without anyone from @acme/billing having seen the first version of the billing service. Every later pull request that touches services/billing/ requests the billing team, because by then the new line is on main.

The reverse case is the protective one. Suppose a later pull request deletes the /services/billing/ line and changes billing code in the same diff. main still holds the line, so @acme/billing is requested on the pull request that removes it. A team cannot be quietly dropped from a change to its own code; it can only be dropped for the changes that follow. That protection holds only if someone also reviews the CODEOWNERS edit, which is why the CODEOWNERS file needs its own owner.

Check the proposed file before it lands#

You can see the proposed version's effect before merging. When you browse files on the pull request's head branch, the shield tooltip shows "information from the CODEOWNERS file for whichever branch in whichever repository you're looking at." CODEOWNERS and forks The errors endpoint takes a ref, so you can check the proposed file for syntax and owner errors against the head commit: List CODEOWNERS errors

gh api "repos/acme/shop/codeowners/errors?ref=HEAD_SHA"

Auditing CODEOWNERS errors covers what that endpoint does and does not report.

What the base-branch rule does not catch#

Reading the base version stops a pull request from choosing its own reviewers. It does nothing about the content of the ownership change. If the repository admins approve the billing line without asking whether the billing team exists and has write access, GitHub assigns no code owner for that path on the next pull request, and nothing blocks it. The rule also does not help a backport: if the release branch's file names a team that has since been reorganized, the backport goes to the old team, and nothing warns you.

The cost#

The rule adds one review round for each ownership change that comes with new code. A team that bundles "create the service" and "make us its owner" in one pull request either waits for the backend team to review billing code it does not own, or splits the change.

How to sequence ownership changes#

  1. Open the CODEOWNERS change on its own, with the new line and a placeholder file such as a README in the new directory. The CODEOWNERS owner approves it.
  2. After it merges, open the pull request with the code. It now requests the new team.
  3. For a removal, do the reverse: let the departing owner approve the last changes that need them, then remove their line in a separate pull request.
  4. When a release branch lives longer than a few weeks, copy ownership changes to it with the same care as code fixes, or backports will be reviewed under the old map.

If splitting is not worth it for a small new directory, say in the pull request description that the new owners have not reviewed it and request their review by hand. The requirement does not ask for them, so nothing else will.

One month of free code review.

New organizations get one calendar month of hosted code review for private and public GitHub repositories. No card or cloud connection required. Eligibility and fair-use terms.

Start free with GitHub

Related reading