Sub topic 1
Why Code Ownership Matters
Prerequisites
This chapter assumes you know the following:
- Basic Git and GitHub use: branches, commits, and pull requests.
- GitHub repository roles and permissions, such as write and admin access.
- GitHub organizations and teams.
You do not need to know CODEOWNERS syntax yet. Later chapters cover it.
The problem ownership solves
In a shared repository, anyone with write access can open a pull request that touches any file. Reviewers are often picked by habit or by whoever is online. Three problems follow:
- A change to a sensitive area, such as authentication code or deployment workflows, gets approved by someone unfamiliar with it.
- Authors do not know whom to ask, so reviews stall.
- Experts see only the changes that people remember to send them.
Code ownership fixes this by recording who is responsible for which paths. GitHub implements it with a plain-text file named CODEOWNERS. Each line maps a path pattern to one or more owners. An owner can be a user, a team, or an email address.
What GitHub does with the mapping
The file by itself is only a record. GitHub uses it in two ways.
- Automatic review requests. When a pull request changes files that match a pattern, GitHub requests a review from the owners of those files. The author does not have to choose reviewers.
- Required approval. If you enable the code owner review requirement on a protected branch, the pull request cannot merge until the owners have approved it.
The first works without any protection settings. The second needs a branch protection rule or a ruleset.
How this differs from a general required reviewer
Branch protection can already require a number of approving reviews. That setting counts approvals from anyone with write access. It cannot say who must approve, or that a change to /docs/ needs the docs team and a change to /apps/web/auth/ needs the security team.
CODEOWNERS adds that path-based rule. The two settings combine. You can require one approval from anyone and also require approval from the relevant owners.
| Setting | Who must approve | Depends on changed paths |
|---|---|---|
| Required approving reviews | Any user with write access, up to the count you set | No |
| Required code owner review | The owners named for the changed files | Yes |
A small example
This file makes the security team responsible for authentication code and the docs team responsible for documentation:
# .github/CODEOWNERS
/apps/web/auth/ @example-org/security
/docs/ @example-org/docs-teamA pull request that edits only /docs/guide.md requests a review from @example-org/docs-team. A pull request that edits both a docs file and a file under /apps/web/auth/ requests both teams.
Costs to keep in mind
Ownership can slow work down if it is too broad. A single owner team on * makes every pull request wait on one group. Later chapters show how to scope patterns and design ownership so unrelated changes are not blocked.
Exercise
A repository requires 2 approving reviews on main, but does not enable code owner review. A pull request changes only files under a path owned by the security team in CODEOWNERS. Can it merge with two approvals from developers outside the security team? Why?
Show answer
Yes. GitHub still requests a review from the security team because of the CODEOWNERS match. Without the code owner review requirement, that approval is not mandatory. The general rule counts approvals from any user with write access, so two approvals from other developers satisfy it.