2019-10-17 14:08:05 -04:00
---
type: reference
---
2019-07-08 04:50:38 -04:00
# Code Owners **(STARTER)**
2019-05-05 11:37:18 -04:00
2020-02-06 10:09:11 -05:00
> - [Introduced](https://gitlab.com/gitlab-org/gitlab/-/merge_requests/6916)
2019-05-05 11:37:18 -04:00
in [GitLab Starter ](https://about.gitlab.com/pricing/ ) 11.3.
2019-09-18 10:02:45 -04:00
> - [Support for group namespaces](https://gitlab.com/gitlab-org/gitlab-foss/issues/53182) added in GitLab Starter 12.1.
2019-10-17 14:08:05 -04:00
> - Code Owners for Merge Request approvals was [introduced](https://gitlab.com/gitlab-org/gitlab/issues/4418) in [GitLab Premium](https://about.gitlab.com/pricing/) 11.9.
2019-05-05 11:37:18 -04:00
2020-04-23 11:09:55 -04:00
## Introduction
When contributing to a project, it can often be difficult
to find out who should review or approve merge requests.
Additionally, if you have a question over a specific file or
code block, it may be difficult to know who to find the answer from.
GitLab Code Owners is a feature to define who owns specific
files or paths in a repository, allowing other users to understand
who is responsible for each file or path.
## Why is this useful?
Code Owners allows for a version controlled single source of
truth file outlining the exact GitLab users or groups that
own certain files or paths in a repository. Code Owners can be
utilized in the merge request approval process which can streamline
the process of finding the right reviewers and approvers for a given
merge request.
In larger organizations or popular open source projects, Code Owners
can also be useful to understand who to contact if you have
a question that may not be related to code review or a merge request
approval.
## How to set up Code Owners
2019-07-04 05:13:50 -04:00
You can use a `CODEOWNERS` file to specify users or
[shared groups ](members/share_project_with_groups.md )
that are responsible for certain files in a repository.
2019-05-05 11:37:18 -04:00
You can choose and add the `CODEOWNERS` file in three places:
2019-10-17 14:08:05 -04:00
- To the root directory of the repository
- Inside the `.gitlab/` directory
- Inside the `docs/` directory
2019-05-05 11:37:18 -04:00
The `CODEOWNERS` file is scoped to a branch, which means that with the
introduction of new files, the person adding the new content can
specify themselves as a code owner, all before the new changes
get merged to the default branch.
When a file matches multiple entries in the `CODEOWNERS` file,
2020-03-06 16:07:59 -05:00
the users from last pattern matching the file are displayed on the
blob page of the given file. For example, you have the following
`CODEOWNERS` file:
2020-03-25 02:07:58 -04:00
```plaintext
2020-03-06 16:07:59 -05:00
README.md @user1
# This line would also match the file README.md
*.md @user2
```
The user that would show for `README.md` would be `@user2` .
2019-05-05 11:37:18 -04:00
2019-10-17 14:08:05 -04:00
## Approvals by Code Owners
Once you've set Code Owners to a project, you can configure it to
2020-04-23 11:09:55 -04:00
be used for merge request approvals:
2019-10-17 14:08:05 -04:00
2020-01-28 22:08:38 -05:00
- As [merge request eligible approvers ](merge_requests/merge_request_approvals.md#code-owners-as-eligible-approvers ).
2019-10-17 14:08:05 -04:00
- As required approvers for [protected branches ](protected_branches.md#protected-branches-approval-by-code-owners-premium ). ** (PREMIUM)**
Once set, Code Owners are displayed in merge requests widgets:
![MR widget - Code Owners ](img/code_owners_mr_widget_v12_4.png )
2020-04-24 11:09:37 -04:00
NOTE: **Note** :
While the`CODEOWNERS` file can be used in addition to Merge Request [Approval Rules ](merge_requests/merge_request_approvals.md#approval-rules ) it can also be used as the sole driver of a Merge Request approval (without using [Approval Rules ](merge_requests/merge_request_approvals.md#approval-rules )) by simply creating the file in one of the three locations specified above, configuring the Code Owners to be required approvers for [protected branches ](protected_branches.md#protected-branches-approval-by-code-owners-premium ) and then using [the syntax of Code Owners files ](code_owners.md#the-syntax-of-code-owners-files ) to specify the actual owners and granular permissions.
2019-05-05 11:37:18 -04:00
## The syntax of Code Owners files
Files can be specified using the same kind of patterns you would use
in the `.gitignore` file followed by the `@username` or email of one
2019-07-04 05:13:50 -04:00
or more users or by the `@name` of one or more groups that should
2020-02-07 10:09:52 -05:00
be owners of the file. Groups must be added as [members of the project ](members/index.md ),
or they will be ignored.
2019-05-05 11:37:18 -04:00
2020-05-20 17:09:09 -04:00
Starting in [GitLab 13.0 ](https://gitlab.com/gitlab-org/gitlab/-/issues/32432 ), you can now specify
groups or subgroups from the project's group hierarchy as potential code owners.
For example, consider the following hierarchy for a given project:
```text
group >> sub-group >> sub-subgroup >> myproject >> file.md
```
Any of the following groups would be eligible to be specified as code owners:
- `@group`
- `@group/sub-group`
- `@group/sub-group/sub-subgroup`
In addition, any groups that have been invited to the project using the **Settings > Members** tool will also be recognized as eligible code owners.
2019-05-05 11:37:18 -04:00
The order in which the paths are defined is significant: the last
pattern that matches a given path will be used to find the code
owners.
Starting a line with a `#` indicates a comment. This needs to be
escaped using `\#` to address files for which the name starts with a
`#` .
Example `CODEOWNERS` file:
2020-03-25 02:07:58 -04:00
```plaintext
2020-04-24 11:09:37 -04:00
# This is an example of a code owners file
# lines starting with a `#` will be ignored.
2019-05-05 11:37:18 -04:00
# app/ @commented-rule
2019-05-20 10:11:44 -04:00
# We can specify a default match using wildcards:
2019-05-05 11:37:18 -04:00
* @default -codeowner
2020-04-24 11:09:37 -04:00
# We can also specify "multiple tab or space" separated codeowners:
* @multiple @code @owners
2019-05-05 11:37:18 -04:00
# Rules defined later in the file take precedence over the rules
# defined before.
# This will match all files for which the file name ends in `.rb`
*.rb @ruby -owner
2020-05-10 08:10:05 -04:00
# Files with a `#` can still be accessed by escaping the pound sign
2019-05-05 11:37:18 -04:00
\#file_with_pound.rb @owner -file-with-pound
2019-07-11 18:53:54 -04:00
# Multiple codeowners can be specified, separated by spaces or tabs
2020-04-24 11:09:37 -04:00
# In the following case the CODEOWNERS file from the root of the repo
# has 3 code owners (@multiple @code @owners)
2019-08-29 22:25:44 -04:00
CODEOWNERS @multiple @code @owners
2019-05-05 11:37:18 -04:00
# Both usernames or email addresses can be used to match
# users. Everything else will be ignored. For example this will
# specify `@legal` and a user with email `janedoe@gitlab.com` as the
# owner for the LICENSE file
LICENSE @legal this_does_not_match janedoe@gitlab.com
2019-07-04 05:13:50 -04:00
# Group names can be used to match groups and nested groups to specify
# them as owners for a file
README @group @group/with -nested/subgroup
2019-05-05 11:37:18 -04:00
# Ending a path in a `/` will specify the code owners for every file
# nested in that directory, on any level
/docs/ @all -docs
# Ending a path in `/*` will specify code owners for every file in
# that directory, but not nested deeper. This will match
# `docs/index.md` but not `docs/projects/index.md`
/docs/* @root -docs
# This will make a `lib` directory nested anywhere in the repository
# match
lib/ @lib -owner
# This will only match a `config` directory in the root of the
# repository
/config/ @config -owner
# If the path contains spaces, these need to be escaped like this:
path\ with\ spaces/ @space -owner
```