info: To determine the technical writer assigned to the Stage/Group associated with this page, see https://about.gitlab.com/handbook/product/ux/technical-writing/#assignments
---
# Code Owners **(PREMIUM)**
> Moved to GitLab Premium in 13.9.
Use the Code Owners feature to define who has expertise for specific parts of your project's codebase.
Define the owners of files and directories in a repository to:
- **Require owners to approve changes.** Combine protected branches with Code Owners to require
experts to approve merge requests before they merge into a protected branch.
- **Identify owners.** Code Owner names are displayed on the files and directories they own:
![Code Owners displayed in UI](../img/codeowners_in_UI_v15_10.png)
Use Code Owners in combination with merge request
[approval rules](../merge_requests/approvals/rules.md) (either optional or required)
to build a flexible approval workflow:
- Use **Code Owners** to ensure quality. Define the users who have domain expertise
for specific paths in your repository.
- Use **Approval rules** to define areas of expertise that don't correspond to specific
file paths in your repository. Approval rules help guide merge request creators to
the correct set of reviewers, such as frontend developers or a security team.
For example:
| Type | Name | Scope | Comment |
|------|------|--------|------------|
| Approval rule | UX | All files | A user experience (UX) team member reviews the user experience of all changes made in your project.
| Approval rule | Security | All files | A security team member reviews all changes for vulnerabilities.
| Code Owner approval rule | Frontend: Code Style | `*.css` files | A frontend engineer reviews CSS file changes for adherence to project style standards.
| Code Owner approval rule | Backend: Code Review | `*.rb` files | A backend engineer reviews the logic and code style of Ruby files.
<divclass="video-fallback">
Video introduction: <ahref="https://www.youtube.com/watch?v=RoyBySTUSB0">Code Owners</a>.
For approval to be required, groups as Code Owners must have a direct membership
(not inherited membership) in the project. Approval can only be optional for groups
that inherit membership. Members in the Code Owners group also must be direct members,
and not inherit membership from any parent groups.
### Define more specific owners for more specifically defined files or directories
When a file or directory matches multiple entries in the `CODEOWNERS` file,
the users from last pattern matching the file or directory are used. This enables you
to define more specific owners for more specifically defined files or directories, when
you order the entries in a sensible way.
For example, in the following `CODEOWNERS` file:
```plaintext
# This line would match the file terms.md
*.md @doc-team
# This line would also match the file terms.md
terms.md @legal-team
```
The Code Owner for `terms.md` would be `@legal-team`.
If you use sections, the last pattern matching the file or directory for each section is used.
For example, in a `CODEOWNERS` file using sections:
```plaintext
[README Owners]
README.md @user1@user2
internal/README.md @user4
[README other owners]
README.md @user3
```
The Code Owners for the `README.md` in the root directory are `@user1`, `@user2`,
and `@user3`. The Code Owners for `internal/README.md` are `@user4` and `@user3`.
Only one CODEOWNERS pattern per section is matched to a file path.
### Organize Code Owners by putting them into sections
> - [Introduced](https://gitlab.com/gitlab-org/gitlab/-/issues/12137) in GitLab 13.2 [with a flag](../../../administration/feature_flags.md) named `sectional_codeowners`. Disabled by default.
> - [Generally available](https://gitlab.com/gitlab-org/gitlab/-/merge_requests/42389) in GitLab 13.4. Feature flag `sectional_codeowners` removed.
You can organize Code Owners by putting them into named sections.
You can use sections for shared directories, so that multiple
teams can be reviewers.
To add a section to the `CODEOWNERS` file, enter a section name in brackets,
followed by the files or directories, and users, groups, or subgroups:
```plaintext
[README Owners]
README.md @user1@user2
internal/README.md @user2
```
Each Code Owner in the merge request widget is listed under a label.
The following image shows a **Groups** and **Documentation** section:
> - [Introduced](https://gitlab.com/gitlab-org/gitlab/-/issues/371711) in GitLab 15.11 [with a flag](../../../administration/feature_flags.md) named `codeowners_default_owners`. Disabled by default.
> - [Generally available](https://gitlab.com/gitlab-org/gitlab/-/merge_requests/115888) in GitLab 15.11. Feature flag `codeowners_default_owners` removed.
If multiple file paths inside a section share the same ownership, define a default
Code Owner for the section. All paths in that section inherit this default, unless
you override the section default on a specific line.
Default owners are applied when specific owners are not specified for file paths.
Specific owners defined beside the file path override default owners:
```plaintext
[Documentation] @docs-team
docs/
README.md
[Database] @database-team
model/db/
config/db/database-setup.md @docs-team
```
In this example:
-`@docs-team` owns all items in the `Documentation` section.
-`@database-team` owns all items in the `Database` section except
`config/db/database-setup.md`, which has an override assigning it to `@docs-team`.
To combine the syntax for default owners with [optional sections](#make-a-code-owners-section-optional)
and required approvals, place default owners at the end:
```plaintext
[Documentation][2] @docs-team
docs/
README.md
^[Database] @database-team
model/db/
config/db/database-setup.md @docs-team
```
#### Sections with duplicate names
If multiple sections have the same name, they are combined.
Also, section headings are not case-sensitive. For example:
```plaintext
[Documentation]
ee/docs/ @docs
docs/ @docs
[Database]
README.md @database
model/db/ @database
[DOCUMENTATION]
README.md @docs
```
This code results in three entries under the **Documentation** section header, and two
entries under **Database**. The entries defined under the sections **Documentation** and
**DOCUMENTATION** are combined, using the case of the first section.
#### Make a Code Owners section optional
> [Introduced](https://gitlab.com/gitlab-org/gitlab/-/issues/232995) in GitLab 13.8.
You can designate optional sections in your Code Owners file. Prepend the
section name with the caret `^` character to treat the entire section as optional.
Optional sections enable you to designate responsible parties for various parts
of your codebase, but not require approval from them. This approach provides
a more relaxed policy for parts of your project that are frequently updated,
but don't require stringent reviews.
In this example, the `[Go]` section is optional:
```plaintext
[Documentation]
*.md @root
[Ruby]
*.rb @root
^[Go]
*.go @root
```
The optional Code Owners section displays in merge requests under the **Approval Rules** area:
to the behavior of this setting. Do not intentionally set invalid values. They may
become valid in the future, and cause unexpected behavior.
Please confirm you enabled `Require approval from code owners` in `Settings > Repository > Protected branches`, otherwise the Code Owner approvals will be optional.
In this example, the `[Documentation]` section requires 2 approvals:
```plaintext
[Documentation][2]
*.md @tech-writer-team
[Ruby]
*.rb @dev-team
```
The `Documentation` Code Owners section under the **Approval Rules** area displays 2 approvals are required:
The Code Owner approval and protected branch features do not apply to users who
are **Allowed to push**.
## Technical Resources
[Code Owners development guidelines](../../../development/code_owners/index.md)
## Troubleshooting
For more information about how the Code Owners feature handles errors, see the
[Code Owners reference](reference.md).
### Approvals shown as optional
A Code Owner approval rule is optional if any of these conditions are true:
- The user or group are not a member of the project. Code Owners [cannot inherit from parent groups](https://gitlab.com/gitlab-org/gitlab/-/issues/288851/).
- [Code Owner approval on a protected branch](../protected_branches.md#require-code-owner-approval-on-a-protected-branch) has not been set up.
- The section is [marked as optional](#make-a-code-owners-section-optional).
### Approvals do not show
Code Owner approval rules only update when the merge request is created.
If you update the `CODEOWNERS` file, close the merge request and create a new one.
### User not shown as possible approver
A user might not show as an approver on the Code Owner merge request approval rules
if any of these conditions are true:
- A rule prevents the specific user from approving the merge request.
Check the project [merge request approval](../merge_requests/approvals/settings.md#edit-merge-request-approval-settings) settings.
- A Code Owner group has a visibility of **private**, and the current user is not a
member of the Code Owner group.
- Current user is an external user who does not have permission to the internal Code Owner group.
### Approval rule is invalid. GitLab has approved this rule automatically to unblock the merge request
This message may appear if an approval rule uses a Code Owner that is not a direct member of the project.
Check that the group or user has been invited to the project.