CODEOWNERS Explained: Patterns, Order, and the Last Match Wins Rule
The short answer: the last matching line wins
In a CODEOWNERS file, when more than one line matches a path, the last one decides who owns it. So you put the broad rules at the top and the specific ones below. GitHub says it plainly: "Order is important; the last matching pattern takes the most precedence."
The patterns look like gitignore patterns, and mostly they are. A few gitignore features are missing, though. That's where files go wrong.
Where the file lives and who can own things
Create a file named CODEOWNERS in .github/, the repository root, or docs/. If more than one exists, GitHub searches in that order and uses the first it finds. The file has to be on the pull request's base branch, it must be under 3 MB, and paths are case sensitive. If you keep other project files in .github/ too, such as a SECURITY.md, CODEOWNERS fits right beside them.
An owner can be a username, a team, or an email address:
*.md @octocat
*.js @my-org/frontend-team
docs/* docs@example.comTwo rules trip people up:
- Owners need explicit
writeaccess to the repository, even if a team's members already have it. Unknown or under-privileged owners are silently not assigned. - Several owners for one pattern must share a line. If they are not on the same line, the pattern matches only the last mentioned owner.
Patterns that work
The pattern rules come from the gitignore documentation. In short:
- A slash at the start or middle anchors the pattern to the root. Without one, the pattern can match at any depth.
- A trailing slash matches directories only.
*matches anything except a slash.**matches across directories.
Here is how that plays out, using examples from GitHub's page:
| Pattern | What it matches | | --- | --- | | * | Everything in the repository | | *.js | JavaScript files at any depth | | /docs/ | Only the root docs directory and its subdirectories | | apps/ | An apps directory at any level | | docs/* | docs/getting-started.md, but not docs/build-app/troubleshooting.md | | **/logs | A logs directory at any depth |
The docs/* row is the one that surprises people. A single star stops at a slash, so anything nested deeper isn't matched. If you want the whole tree, use /docs/. If you document code in a structured way, for example sorting docs into four kinds, a directory pattern keeps each kind with one owner.
Last match wins, in a worked example
Say you want a default owner for everything, a JavaScript owner, and a separate owner for one folder:
# Default owner for everything
* @global-owner
# JavaScript files
*.js @js-owner
# The build output folder has its own owner
/build/logs/ @ops-teamA pull request that changes only a .js file asks @js-owner, not @global-owner, because *.js comes after * and matches last. A change to /build/logs/server.js asks @ops-team. Reverse the order and the catch-all * matches last for every file. The specific rules would never apply.
You can also use this to hand a path back. A later pattern with no owner leaves that path to anyone with write access: GitHub's example lists /apps/github with no owner after /apps/ @octocat, and approval from any user with write access is then enough.
Other plain-text rule files have their own precedence rules, and they don't all match this one. robots.txt is a good example to compare against.
What does not carry over from gitignore
GitHub lists three gitignore features that do not work in CODEOWNERS:
- Escaping a leading
#with a backslash so it is a pattern, not a comment. - Negating a pattern with
!. - Character ranges in
[ ].
If a line has invalid syntax, GitHub skips it, so a typo can quietly remove an owner. Check the highlighted errors after every edit. Because there is no negation, the way to carve out an exception is a later, more specific line, as in the example above.
Making reviews required
A CODEOWNERS file on its own requests reviews. To make them required, turn on "Require review from Code Owners" in a branch protection rule. GitHub's protected branches page says that with this on, any pull request that affects code with a code owner must be approved by that code owner before it can merge into the protected branch. When several owners share a line, approval from any one of them is enough.
By default, branch protection does not apply to people with admin permissions. GitHub also suggests protecting the file itself with a line such as /.github/CODEOWNERS @owner_username.
How Carets fits in
A CODEOWNERS file is a small plain-text file you edit often and want to get exactly right: one stray space or a wrong slash changes who gets asked to review. Carets is a native notes and code editor for iPhone, iPad, and Mac that opens plain text and config files with syntax highlighting, so comments and patterns are easy to scan. You can keep a rules file next to your notes, organized into files, projects, and tags, and draft a pattern list before you commit it. Your files stay plain text, so they work with Git and any other tool.
Get Carets on the App Store for iPhone, iPad, and Mac.
Frequently Asked Questions
Where should the CODEOWNERS file go?
In .github/, the repository root, or docs/. GitHub uses the first one it finds in that order, and the file must be on the pull request's base branch.
Can I negate a pattern with ! in CODEOWNERS?
No. Negation, escaping a leading #, and [ ] ranges do not work in CODEOWNERS. Use a later, more specific line instead.
Do all listed owners have to approve?
No. With required code owner review on, approval from any one owner on the matching line is enough.