Data report"State of code review 2024" is now liveRead the full report

Understanding GitHub CODEOWNERS

Kenny DuMez
Kenny DuMez
Graphite software engineer


Note

This guide explains this concept in vanilla Git. For Graphite documentation, see our CLI docs.


GitHub's CODEOWNERS is a tool for managing and streamlining the code review process in large projects. By using a CODEOWNERS file, you can automatically assign review responsibilities to specific team members for particular parts of your project. This guide will explain the concept of GitHub CODEOWNERS, show you how to set up a CODEOWNERS file, and discuss how to effectively implement it in your project workflows.

The term "code owners" refers to individuals or teams specified in a repository who are responsible for maintaining certain files or directories within the project. When changes are proposed to those areas, the designated code owners are automatically requested for review. This ensures that the right people, who are most familiar with the specific parts of the code the changes are referencing, are involved in the decision-making and review process.

To start using the CODEOWNERS feature, you need to create a file named CODEOWNERS in your repository. This file can be placed in one of three locations:

  • The root of the repository
  • The .github directory
  • The docs directory

GitHub will use the file in the first location it finds on the default branch (typically "main").

The CODEOWNERS file uses a pattern format similar to .gitignore files. Each line in a CODEOWNERS file assigns one or more codeowners to a set of files using glob patterns. Here is a simple example:

Terminal
# This is a comment.
* @username1
/docs @username2
*.js @dev-team
  • The asterisk * represents all files in the repository, and @username1 is assigned as the codeowner.
  • /docs specifies that @username2 will be the codeowner of all files under the docs directory.
  • *.js indicates that any JavaScript files in the repository are owned by the @dev-team.

The CODEOWNERS file follows specific pattern rules and precedence:

  • Last matching pattern takes precedence: When multiple patterns apply to the same file, the last pattern listed in the CODEOWNERS file takes precedence. This means that if two patterns refer to the same file, the owner specified in the pattern that appears later in the file will be responsible for reviewing changes to that file.

  • Using named groups: Teams are usually defined within the organization or repository settings on GitHub. After a team is created there, it can be referenced in the CODEOWNERS file using the team's handle, like @org/team-name.

As an example:

Terminal
# Example CODEOWNERS file
* @global-owner1
/docs/ @documentation-team
/src/ @dev-team
/src/special-file.txt @special-owner
  • @global-owner1 is the default owner for all files unless a more specific pattern applies.
  • Any file in the /docs/ directory will have @documentation-team as its code owner.
  • Any file in the /src/ directory will have @dev-team as its code owner, except special-file.txt, which will be owned by @special-owner because its pattern comes later in the file and specifically matches that file.

Effective use of the CODEOWNERS file can greatly improve the management of your project. Here are some best practices:

  • Regularly update the CODEOWNERS file as team members change roles or leave.
  • Use groups instead of individual usernames when possible to reduce the need for frequent updates.
  • Combine CODEOWNERS with branch protection rules to ensure that PRs must be reviewed before merge.

A CODEOWNERS file can also be a security tool. By assigning sensitive parts of your codebase (like configuration files or backend logic) to senior developers or security teams, you can add an additional layer of scrutiny to changes in critical areas.

By implementing a CODEOWNERS file, projects can ensure that all parts of their codebase are reviewed by the individuals with the most expertise and responsibility for those areas. This not only improves code quality but also helps in faster, more efficient development cycles.

To go a step further, use Graphite protections for more flexible and secure branch protections.

Screenshot of configuring Graphite protections

Graphite Protections offers a significant enhancement over traditional CODEOWNERS and branch protection mechanisms, providing greater security and flexibility for development teams, especially those using monorepos.

Unlike the static assignment of CODEOWNERS and the limited flexibility of branch protection rules, Graphite Protections allows teams to define custom merge requirements tailored to specific directories, authors, and types of changes, thereby accommodating the unique needs of different teams within the same organization. This system supports nuanced development practices by allowing overrides under certain conditions while maintaining high standards for code quality.

For instance, Graphite Protections enables PR authors to bypass certain rules, like merging large PRs that are not code modifications or reverts, while still enforcing overall code quality standards. This approach not only ensures adherence to company policies but also streamlines the code review process, making it faster and more efficient.

For further reading on GitHub CODEOWNERS see the official GitHub documentation.

Graphite
Git stacked on GitHub

Stacked pull requests are easier to read, easier to write, and easier to manage.
Teams that stack ship better software, faster.

Or install our CLI.
Product Screenshot 1
Product Screenshot 2