Sub topic 4
Writing path patterns
This chapter assumes you can read .gitignore-style globs, you know where a CODEOWNERS file lives, and you can write an owner such as @example-org/docs-team. Here you will learn how each line's pattern selects files.
The shape of a rule
Each rule is a path pattern followed by one or more owners, separated by whitespace. Lines starting with # are comments. CODEOWNERS patterns follow most .gitignore rules, with a few differences covered below.
# .github/CODEOWNERS
# Fallback owner for everything not matched below
* @example-org/maintainers
# Any JavaScript file anywhere in the repository
*.js @example-org/frontend
# Everything under the top-level docs directory, including subdirectories
/docs/ @example-org/docs-team [email protected]
# Only files directly inside /config, not nested directories
/config/* @example-org/platform
# A specific user owns the build scripts
/scripts/build/ @octocatAnchoring and directories
A leading slash anchors the pattern to the repository root. Without it, the pattern can match at any depth.
| Pattern | What it matches |
|---|---|
| `*` | Every file in the repository |
| `*.js` | Any `.js` file at any depth |
| `/docs/` | The top-level `docs` directory and everything beneath it |
| `docs/` | A directory named `docs` at any depth, with everything beneath it |
| `/config/*` | Files directly inside `/config`, but not files in its subdirectories |
| `docs/*` | Files directly inside any `docs` directory, but not deeper files |
A trailing slash means "this directory and everything inside it". A trailing /* means only the direct children.
Wildcards you can use
*matches any run of characters within one path segment.**matches across directories. For example,/apps/**/tests/matches atestsdirectory at any depth under/apps.?matches a single character.
Differences from .gitignore
Some .gitignore features are not supported in CODEOWNERS:
- Escaping a leading
#with\so it is treated as a pattern does not work. A line starting with#is always a comment. !does not negate a pattern.[ ]character ranges do not work.
A pattern that uses one of these is treated as invalid, and GitHub reports a syntax error. Chapter 14 covers how to read those errors.
Patterns are also case-sensitive. /Docs/ will not match a directory named docs.
Common mistakes
- Writing
docs/*and expecting nested files to be covered. Usedocs/instead. - Forgetting the leading slash and matching a nested directory with the same name, such as
vendor/docs/. - Putting spaces in a path. Whitespace separates the pattern from the owners, so escape each space with a backslash, as in
/my\ folder/.
When several lines match one file, only one of them applies. That rule is the next chapter's subject.
Exercise
A repository has the rule /config/* @example-org/platform. Who owns config/prod/db.yml, and how would you change the pattern so that file is covered by the platform team?
Show answer
Nobody owns it through this rule, because /config/* matches only files directly inside /config, and config/prod/db.yml is nested. Change the pattern to /config/ so it covers the directory and everything beneath it.
Exercise
What is the difference between docs/ and /docs/?
Show answer
/docs/ is anchored to the repository root and matches only the top-level docs directory. docs/ has no leading slash, so it also matches docs directories at any depth, such as packages/api/docs/.