debian-mirror-gitlab/doc/user/project/protected_branches.md

243 lines
11 KiB
Markdown
Raw Normal View History

2019-10-12 21:52:04 +05:30
---
2020-10-24 23:57:45 +05:30
stage: Create
group: Source Code
2022-03-02 08:16:31 +05:30
info: To determine the technical writer assigned to the Stage/Group associated with this page, see https://about.gitlab.com/handbook/engineering/ux/technical-writing/#assignments
2019-10-12 21:52:04 +05:30
---
2021-03-11 19:13:27 +05:30
# Protected branches **(FREE)**
2016-08-24 12:49:21 +05:30
2021-06-08 01:23:25 +05:30
In GitLab, [permissions](../permissions.md) are fundamentally defined around the
2019-12-04 20:38:33 +05:30
idea of having read or write permission to the repository and branches. To impose
further restrictions on certain branches, they can be protected.
2016-08-24 12:49:21 +05:30
2021-06-08 01:23:25 +05:30
The default branch for your repository is protected by default.
2016-09-13 17:45:13 +05:30
2021-09-30 23:02:18 +05:30
## Who can modify a protected branch
2016-08-24 12:49:21 +05:30
2021-09-30 23:02:18 +05:30
When a branch is protected, the default behavior enforces these restrictions on the branch.
2016-08-24 12:49:21 +05:30
2021-09-30 23:02:18 +05:30
| Action | Who can do it |
|:-------------------------|:------------------------------------------------------------------|
2022-04-04 11:22:00 +05:30
| Protect a branch | At least the Maintainer role. |
2021-09-30 23:02:18 +05:30
| Push to the branch | GitLab administrators and anyone with **Allowed** permission. (1) |
| Force push to the branch | No one. |
| Delete the branch | No one. |
2016-08-24 12:49:21 +05:30
2021-09-30 23:02:18 +05:30
1. Users with the Developer role can create a project in a group, but might not be allowed to
initially push to the [default branch](repository/branches/default.md).
2016-09-13 17:45:13 +05:30
2021-09-30 23:02:18 +05:30
### Set the default branch protection level
2019-12-21 20:55:43 +05:30
2021-09-30 23:02:18 +05:30
Administrators can set a default branch protection level in the
2022-05-07 20:08:51 +05:30
[Admin Area](../project/repository/branches/default.md#instance-level-default-branch-protection).
2021-03-11 19:13:27 +05:30
2021-06-08 01:23:25 +05:30
## Configure a protected branch
2016-08-24 12:49:21 +05:30
2021-06-08 01:23:25 +05:30
Prerequisite:
2016-08-24 12:49:21 +05:30
2022-04-04 11:22:00 +05:30
- You must have at least the Maintainer role.
2016-08-24 12:49:21 +05:30
2021-06-08 01:23:25 +05:30
To protect a branch:
2016-08-24 12:49:21 +05:30
2021-06-08 01:23:25 +05:30
1. Go to your project and select **Settings > Repository**.
1. Expand **Protected branches**.
2022-05-07 20:08:51 +05:30
1. From the **Branch** dropdown list, select the branch you want to protect.
2021-09-30 23:02:18 +05:30
1. From the **Allowed to merge** list, select a role, or group that can merge into this branch. In GitLab Premium, you can also add users.
1. From the **Allowed to push** list, select a role, group, or user that can push to this branch. In GitLab Premium, you can also add users.
2021-06-08 01:23:25 +05:30
1. Select **Protect**.
2016-08-24 12:49:21 +05:30
2021-09-30 23:02:18 +05:30
The protected branch displays in the list of protected branches.
2016-08-24 12:49:21 +05:30
2021-09-30 23:02:18 +05:30
## Configure multiple protected branches by using a wildcard
2016-09-13 17:45:13 +05:30
2021-09-30 23:02:18 +05:30
Prerequisite:
2021-02-22 17:27:13 +05:30
2022-04-04 11:22:00 +05:30
- You must have at least the Maintainer role.
2021-02-22 17:27:13 +05:30
2021-09-30 23:02:18 +05:30
To protect multiple branches at the same time:
2021-02-22 17:27:13 +05:30
2021-09-30 23:02:18 +05:30
1. Go to your project and select **Settings > Repository**.
1. Expand **Protected branches**.
2022-05-07 20:08:51 +05:30
1. From the **Branch** dropdown list, type the branch name and a wildcard.
2021-09-30 23:02:18 +05:30
For example:
2021-02-22 17:27:13 +05:30
2021-09-30 23:02:18 +05:30
| Wildcard protected branch | Matching branches |
|---------------------------|--------------------------------------------------------|
| `*-stable` | `production-stable`, `staging-stable` |
| `production/*` | `production/app-server`, `production/load-balancer` |
| `*gitlab*` | `gitlab`, `gitlab/staging`, `master/gitlab/production` |
2021-02-22 17:27:13 +05:30
2021-09-30 23:02:18 +05:30
1. From the **Allowed to merge** list, select a role, or group that can merge into this branch. In GitLab Premium, you can also add users.
1. From the **Allowed to push** list, select a role, group, or user that can push to this branch. In GitLab Premium, you can also add users.
1. Select **Protect**.
2021-02-22 17:27:13 +05:30
2021-09-30 23:02:18 +05:30
The protected branch displays in the list of protected branches.
2019-07-31 22:56:46 +05:30
2021-09-30 23:02:18 +05:30
## Create a protected branch
2019-07-31 22:56:46 +05:30
2022-04-04 11:22:00 +05:30
Users with at least the Developer role can create a protected branch.
2019-07-31 22:56:46 +05:30
2021-09-30 23:02:18 +05:30
Prerequisites:
2019-07-31 22:56:46 +05:30
2021-09-30 23:02:18 +05:30
- **Allowed to push** is set to **No one**
- **Allowed to merge** is set to **Developers**.
2019-07-31 22:56:46 +05:30
2021-09-30 23:02:18 +05:30
You can create a protected branch by using the UI or API only.
This prevents you from accidentally creating a branch
from the command line or from a Git client application.
2016-08-24 12:49:21 +05:30
2021-09-30 23:02:18 +05:30
To create a new branch through the user interface:
2016-08-24 12:49:21 +05:30
2021-09-30 23:02:18 +05:30
1. Go to **Repository > Branches**.
1. Select **New branch**.
1. Fill in the branch name and select an existing branch, tag, or commit to
base the new branch on. Only existing protected branches and commits
that are already in protected branches are accepted.
2016-08-24 12:49:21 +05:30
2021-09-30 23:02:18 +05:30
## Require everyone to submit merge requests for a protected branch
2016-08-24 12:49:21 +05:30
2021-09-30 23:02:18 +05:30
You can force everyone to submit a merge request, rather than allowing them to check in directly
to a protected branch. This is compatible with workflows like the [GitLab workflow](../../topics/gitlab_flow.md).
2016-08-24 12:49:21 +05:30
2021-09-30 23:02:18 +05:30
1. Go to your project and select **Settings > Repository**.
1. Expand **Protected branches**.
2022-05-07 20:08:51 +05:30
1. From the **Branch** dropdown list, select the branch you want to protect.
2021-09-30 23:02:18 +05:30
1. From the **Allowed to merge** list, select **Developers + Maintainers**.
1. From the **Allowed to push** list, select **No one**.
1. Select **Protect**.
2016-08-24 12:49:21 +05:30
2021-09-30 23:02:18 +05:30
## Allow everyone to push directly to a protected branch
2016-08-24 12:49:21 +05:30
2021-09-30 23:02:18 +05:30
You can allow everyone with write access to push to the protected branch.
2019-07-07 11:18:12 +05:30
2021-09-30 23:02:18 +05:30
1. Go to your project and select **Settings > Repository**.
1. Expand **Protected branches**.
2022-05-07 20:08:51 +05:30
1. From the **Branch** dropdown list, select the branch you want to protect.
2021-09-30 23:02:18 +05:30
1. From the **Allowed to push** list, select **Developers + Maintainers**.
1. Select **Protect**.
2019-07-07 11:18:12 +05:30
2021-09-30 23:02:18 +05:30
## Allow deploy keys to push to a protected branch
2019-07-07 11:18:12 +05:30
2021-09-30 23:02:18 +05:30
> - [Introduced](https://gitlab.com/gitlab-org/gitlab/-/issues/30769) in GitLab 13.7.
> - This feature was selectively deployed in GitLab.com 13.7, and may not be available for all users.
> - This feature is available for all users in GitLab 13.9.
2019-07-07 11:18:12 +05:30
2021-09-30 23:02:18 +05:30
You can permit the owner of a [deploy key](deploy_keys/index.md) to push to a protected branch.
The deploy key works, even if the user isn't a member of the related project. However, the owner of the deploy
key must have at least read access to the project.
2019-07-07 11:18:12 +05:30
2021-09-30 23:02:18 +05:30
Prerequisites:
2017-09-10 17:25:29 +05:30
2022-05-07 20:08:51 +05:30
- The deploy key must be enabled for your project. A project deploy key is enabled by default when
it is created. However, a public deploy key must be
[granted](deploy_keys/index.md#grant-project-access-to-a-public-deploy-key) access to the
project.
- The deploy key must have [write access](deploy_keys/index.md#permissions) to your project
repository.
2017-09-10 17:25:29 +05:30
2021-09-30 23:02:18 +05:30
To allow a deploy key to push to a protected branch:
2017-09-10 17:25:29 +05:30
2021-09-30 23:02:18 +05:30
1. Go to your project and select **Settings > Repository**.
1. Expand **Protected branches**.
2022-05-07 20:08:51 +05:30
1. From the **Branch** dropdown list, select the branch you want to protect.
2021-09-30 23:02:18 +05:30
1. From the **Allowed to push** list, select the deploy key.
1. Select **Protect**.
2017-09-10 17:25:29 +05:30
2022-05-07 20:08:51 +05:30
Deploy keys are not available in the **Allowed to merge** dropdown list.
2017-09-10 17:25:29 +05:30
2021-09-30 23:02:18 +05:30
## Allow force push on a protected branch
2021-04-17 20:07:23 +05:30
2021-04-29 21:17:54 +05:30
> - [Introduced](https://gitlab.com/gitlab-org/gitlab/-/issues/15611) in GitLab 13.10 behind a disabled feature flag.
2021-09-04 01:27:46 +05:30
> - [Feature flag removed](https://gitlab.com/gitlab-org/gitlab/-/issues/323431) in GitLab 14.0.
2021-04-17 20:07:23 +05:30
2021-09-04 01:27:46 +05:30
You can allow [force pushes](../../topics/git/git_rebase.md#force-push) to
2021-09-30 23:02:18 +05:30
protected branches.
2021-04-17 20:07:23 +05:30
2021-09-30 23:02:18 +05:30
To protect a new branch and enable force push:
2021-04-17 20:07:23 +05:30
2021-09-30 23:02:18 +05:30
1. Go to your project and select **Settings > Repository**.
1. Expand **Protected branches**.
2022-05-07 20:08:51 +05:30
1. From the **Branch** dropdown list, select the branch you want to protect.
2021-09-30 23:02:18 +05:30
1. From the **Allowed to push** and **Allowed to merge** lists, select the settings you want.
1. To allow all users with push access to force push, turn on the **Allowed to force push** toggle.
1. To reject code pushes that change files listed in the `CODEOWNERS` file, turn on the
**Require approval from code owners** toggle.
1. Select **Protect**.
2021-04-17 20:07:23 +05:30
2021-09-30 23:02:18 +05:30
To enable force pushes on branches that are already protected:
2021-04-17 20:07:23 +05:30
2021-09-30 23:02:18 +05:30
1. Go to your project and select **Settings > Repository**.
1. Expand **Protected branches**.
1. In the list of protected branches, next to the branch, turn on the **Allowed to force push** toggle.
2021-04-17 20:07:23 +05:30
2022-05-07 20:08:51 +05:30
Members who can push to this branch can now also force push.
2021-04-17 20:07:23 +05:30
2021-09-30 23:02:18 +05:30
## Require Code Owner approval on a protected branch **(PREMIUM)**
2019-12-21 20:55:43 +05:30
2022-05-07 20:08:51 +05:30
> - [Introduced](https://gitlab.com/gitlab-org/gitlab/-/issues/13251) in GitLab 12.4.
> - [Introduced](https://gitlab.com/gitlab-org/gitlab/-/issues/35097) in GitLab 13.5, users and groups who can push to protected branches do not have to use a merge request to merge their feature branches. This means they can skip merge request approval rules.
2019-12-21 20:55:43 +05:30
2021-10-27 15:23:28 +05:30
For a protected branch, you can require at least one approval by a [Code Owner](code_owners.md).
2019-12-21 20:55:43 +05:30
To protect a new branch and enable Code Owner's approval:
2021-09-30 23:02:18 +05:30
1. Go to your project and select **Settings > Repository**.
1. Expand **Protected branches**.
2022-05-07 20:08:51 +05:30
1. From the **Branch** dropdown list, select the branch you want to protect.
2021-09-30 23:02:18 +05:30
1. From the **Allowed to push** and **Allowed to merge** lists, select the settings you want.
1. Turn on the **Require approval from code owners** toggle.
1. Select **Protect**.
2019-12-21 20:55:43 +05:30
2021-09-30 23:02:18 +05:30
To enable Code Owner's approval on branches that are already protected:
2019-12-21 20:55:43 +05:30
2021-09-30 23:02:18 +05:30
1. Go to your project and select **Settings > Repository**.
1. Expand **Protected branches**.
1. In the list of protected branches, next to the branch, turn on the **Code owner approval** toggle.
2019-12-21 20:55:43 +05:30
2021-09-30 23:02:18 +05:30
When enabled, all merge requests for these branches require approval
2019-12-21 20:55:43 +05:30
by a Code Owner per matched rule before they can be merged.
Additionally, direct pushes to the protected branch are denied if a rule is matched.
2021-10-27 15:23:28 +05:30
Any user who is not specified in the `CODEOWNERS` file cannot push
changes for the specified files or paths, unless they are specifically allowed to.
You don't have to restrict developers from pushing directly to the
protected branch. Instead, you can restrict pushing to certain files where a review by
Code Owners is required.
In [GitLab Premium 13.5 and later](https://gitlab.com/gitlab-org/gitlab/-/issues/35097), users and groups
who are allowed to push to protected branches do not need a merge request to merge their feature branches.
Thus, they can skip merge request approval rules, Code Owners included.
2021-09-30 23:02:18 +05:30
## Run pipelines on protected branches
2021-01-03 14:25:43 +05:30
2021-09-30 23:02:18 +05:30
The permission to merge or push to protected branches defines
whether or not a user can run CI/CD pipelines and execute actions on jobs.
2018-03-17 18:26:18 +05:30
2020-04-22 19:07:51 +05:30
See [Security on protected branches](../../ci/pipelines/index.md#pipeline-security-on-protected-branches)
2018-03-17 18:26:18 +05:30
for details about the pipelines security model.
2021-09-30 23:02:18 +05:30
## Delete a protected branch
2022-04-04 11:22:00 +05:30
Users with at least the Maintainer role can manually delete protected
2021-09-30 23:02:18 +05:30
branches by using the GitLab web interface:
1. Go to **Repository > Branches**.
2022-05-07 20:08:51 +05:30
1. Next to the branch you want to delete, select **Delete** (**{remove}**).
2021-09-30 23:02:18 +05:30
1. On the confirmation dialog, type the branch name and select **Delete protected branch**.
2016-08-24 12:49:21 +05:30
2022-04-04 11:22:00 +05:30
Protected branches can only be deleted by using GitLab either from the UI or API.
This prevents accidentally deleting a branch through local Git commands or
third-party Git clients.
2019-10-12 21:52:04 +05:30
<!-- ## Troubleshooting
Include any troubleshooting steps that you can foresee. If you know beforehand what issues
one might have when setting this up, or when something is changed, or on upgrading, it's
important to describe those, too. Think of things that may go wrong and include them here.
This is important to minimize requests for support, and to avoid doc comments with
questions that you know someone might ask.
Each scenario can be a third-level heading, e.g. `### Getting error message X`.
If you have none to add when creating a doc, leave this section in place
but commented out to help encourage others to add to it in the future. -->