Sub topic 5
When Several Patterns Match: Last Match Wins
This chapter assumes you can read a basic CODEOWNERS file, know the .gitignore-style patterns it uses, and understand GitHub teams and pull requests.
The Rule
A single file can match many patterns. A file such as /apps/web/auth/login.js matches *, *.js, /apps/web/ and /apps/web/auth/ all at once. GitHub does not combine these owners. It uses the owners from the last matching pattern in the file, and it ignores every earlier match.
This is the opposite of some tools where the most specific rule wins. In CODEOWNERS, only position matters. A broad pattern placed at the bottom overrides a narrow pattern placed above it.
A Worked Example
# Later lines override earlier lines
* @example-org/maintainers
/apps/web/ @example-org/web-team
/apps/web/auth/ @example-org/security
# Clear ownership for generated files (no owner listed)
/apps/web/generated/GitHub resolves these paths as follows:
| Changed file | Last matching pattern | Owner |
|---|---|---|
| `README.md` | `*` | `@example-org/maintainers` |
| `apps/web/app.js` | `/apps/web/` | `@example-org/web-team` |
| `apps/web/auth/login.js` | `/apps/web/auth/` | `@example-org/security` |
| `apps/web/generated/api.js` | `/apps/web/generated/` | None |
The security team owns login.js alone. The web team is not added as an owner, even though /apps/web/ also matches.
Ordering Mistakes
The most common error is putting the fallback * line at the bottom. It matches every file, so it would replace all the specific rules above it. Always put the broadest pattern first and the most specific patterns last.
A second error is placing *.js after a directory rule. If *.js @example-org/frontend comes after /apps/web/auth/, every JavaScript file in the auth directory is owned by the frontend team, and security loses it.
# Wrong: the extension rule overrides the auth rule for .js files
/apps/web/auth/ @example-org/security
*.js @example-org/frontendPut *.js first to fix it.
Sharing Ownership
Because owners from different matching lines are never merged, you must list every owner on the winning line if you want several parties to review. This line gives both teams ownership of auth files:
/apps/web/auth/ @example-org/security @example-org/web-teamOwners on one line are alternatives for a request. Whether one or all of them must approve is covered in the chapter on how code owner approval is satisfied.
Exercise
A CODEOWNERS file contains these lines in order: /docs/ @example-org/docs-team, then *.md @example-org/writers. Who owns docs/guide.md, and how would you let the docs team own all files under /docs/, including Markdown files?
Show answer
The writers team owns it, because *.md is the last matching pattern. To give the docs team all files under /docs/, move the /docs/ line below the *.md line.
Exercise
The file /src/api/v1.py matches *, /src/ and /src/api/. The /src/ line is last in the file, after /src/api/. Which owners apply?
Show answer
Only the owners on the /src/ line apply. It is the last matching pattern, so the more specific /src/api/ rule is ignored.