Learning center

Which gitignore habits fail in a CODEOWNERS file?

CODEOWNERS follows most gitignore rules but not three: ! negation, [ ] character ranges and a \# escape for paths that start with #. Lines that use them do not do what a gitignore reader expects. Replace negation with a later line, ranges with one line per path, and keep paths case-exact.

What the documentation says#

GitHub's CODEOWNERS page opens its syntax section with a warning: "There are some syntax rules for gitignore files that do not work in CODEOWNERS files." It lists three:

  • Escaping a pattern starting with # using \ so it is treated as a pattern and not a comment doesn't work
  • Using ! to negate a pattern doesn't work
  • Using [ ] to define a character range doesn't work

CODEOWNERS syntax

The same section adds a fourth difference that a gitignore habit can hide: "CODEOWNERS paths are case sensitive, because GitHub uses a case sensitive file system. Since CODEOWNERS are evaluated by GitHub, even systems that are case insensitive (for example, macOS) must use paths and files that are cased correctly in the CODEOWNERS file."

The documentation says these forms do not work. It does not say whether GitHub reports each one as an error or reads it some other way, and it says separately that "If any line in your CODEOWNERS file contains invalid syntax, that line will be skipped." Plan for the line having no effect, and do not count on a warning.

A fictional file written from gitignore habit#

Suppose the fictional acme/shop repository wants @acme/backend to own services/, the documentation team to own the docs inside it, and @acme/core to own the auth and billing services. Someone used to gitignore writes:

/services/                @acme/backend
!/services/docs/
/services/[ab]*/          @acme/core
\#runbook.md              @acme/sre

Each of the last three lines fails in its own way.

The negation line is meant to release services/docs/ from the backend team. Negation does not work, so the backend team stays the last match for those files, and a docs-only change still requests @acme/backend and, under required code owner review, waits for it.

The range line is meant to match services/auth/ and services/billing/. Ranges do not work, so neither directory gets @acme/core; both stay with @acme/backend. With code owner review required, the core team's review of authentication code is silently absent.

The escaped line is meant to own a file named #runbook.md. In CODEOWNERS a line that starts with # is a comment, and the backslash escape does not change that, so the file has no owner from this line.

None of these produces a merge that GitHub refuses. Each one moves review to a team other than the one intended, or to none.

The rewrite#

GitHub's own example shows the replacement for negation: a later line for the subdirectory, with either different owners or none. About code owners: example file

/services/                @acme/backend
/services/docs/           @acme/docs
/services/auth/           @acme/core
/services/billing/        @acme/core
/ops/                     @acme/sre

The docs line comes after /services/, so it wins for files under services/docs/. The two service directories each get a line instead of a range. The runbook is owned through its directory, ops/ in this example, since no pattern can start with an escaped #. If you want the docs path to have no owner at all, write /services/docs/ with nothing after it; leaving a path unowned covers when that is the right choice.

A real use of the comment rule#

CockroachDB uses the comment rule on purpose. Its header explains that "you can opt out of GitHub-requested code review assignments (while maintaining team ownership) prefixing the line with #!," and that this "will hide the line from GitHub, but our internal tooling continues to parse it." cockroachdb/cockroach .github/CODEOWNERS, read on 2026-11-23. Lines such as #!/pkg/sql/ @cockroachdb/sql-queries-noreview are comments to GitHub. The same mechanism that defeats an escaped # lets one file serve two readers.

What a correct pattern does not catch#

Getting the syntax right only makes each line match what you meant. It does not check that the owner exists and has write access, which the errors audit covers, or that a later line does not override it. A pattern that works in gitignore may also match more than intended, since an unanchored docs/ matches a docs directory anywhere in the tree.

What the rewrite costs#

Without ranges, a set of similar directories needs one line each, and a new directory needs a new line. In a large monorepo that adds lines a reviewer has to read, and lines to keep in step with the tree. Kibana handles this by generating the top of its file from package metadata, with a header that says it "is automatically updated by CI." elastic/kibana .github/CODEOWNERS, read on 2026-11-23.

When the rule needs negation#

If the requirement is naturally "this team reviews everything here except these files," CODEOWNERS can only express it as an override line per exception. The ruleset rule for required reviewers accepts negation: its file patterns use the .gitignore format, and "A pattern starting with an exclamation mark (!) is a negation." Required reviewers GitHub announced negation support, added during the public preview, when the rule became generally available in February 2026. Required reviewer rule changelog Requiring reviewers by path with a ruleset compares the two.

Before you rely on CODEOWNERS for a review requirement, search the file for lines starting with ! or \# and for [, and rewrite each one as above.

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