2019-07-29 04:10:24 +00:00
---
2020-07-28 18:09:36 +00:00
stage: Create
group: Source Code
2020-11-26 06:09:20 +00:00
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-07-29 04:10:24 +00:00
type: reference, howto
---
2021-02-09 18:09:59 +00:00
# Protected branches **(FREE)**
2016-07-22 07:56:25 +00:00
[Permissions ](../permissions.md ) in GitLab are fundamentally defined around the
2019-09-12 02:33:32 +00:00
idea of having read or write permission to the repository and branches. To impose
further restrictions on certain branches, they can be protected.
2016-07-22 07:56:25 +00:00
2016-08-21 07:59:19 +00:00
## Overview
2021-01-29 15:09:40 +00:00
By default, a protected branch does these things:
2016-07-22 07:56:25 +00:00
2019-07-29 04:10:24 +00:00
- It prevents its creation, if not already created, from everybody except users
with Maintainer permission.
2019-08-28 07:16:25 +00:00
- It prevents pushes from everybody except users with **Allowed** permission.
2019-07-29 04:10:24 +00:00
- It prevents **anyone** from force pushing to the branch.
- It prevents **anyone** from deleting the branch.
2016-07-22 07:56:25 +00:00
2021-02-01 18:09:17 +00:00
**Permissions:**
2016-07-22 07:56:25 +00:00
2021-02-01 18:09:17 +00:00
- GitLab administrators are allowed to push to the protected branches.
- Users with [Developer permissions ](../permissions.md ) are allowed to
create a project in a group, but might not be allowed to initially
2021-03-17 21:11:29 +00:00
push to the [default branch ](repository/branches/default.md ).
2016-08-21 07:59:19 +00:00
2019-09-30 00:06:04 +00:00
The default branch protection level is set in the [Admin Area ](../admin_area/settings/visibility_and_access_controls.md#default-branch-protection ).
2021-02-01 18:09:17 +00:00
See the [Changelog ](#changelog ) section for changes over time.
2016-07-22 07:56:25 +00:00
## Configuring protected branches
2021-01-29 15:09:40 +00:00
To protect a branch, you need to have at least Maintainer permission level.
2021-03-11 18:09:23 +00:00
The default branch for your repository is protected by default.
2016-07-22 07:56:25 +00:00
2021-01-29 15:09:40 +00:00
1. In your project, go to **Settings > Repository** .
2017-06-06 10:45:55 +00:00
1. Scroll to find the **Protected branches** section.
2016-07-22 07:56:25 +00:00
1. From the **Branch** dropdown menu, select the branch you want to protect and
click **Protect** . In the screenshot below, we chose the `develop` branch.
2019-08-28 13:09:10 +00:00
![Protected branches page ](img/protected_branches_page_v12_3.png )
2016-07-22 07:56:25 +00:00
2021-01-29 15:09:40 +00:00
1. Once done, the protected branch displays in the **Protected branches** list.
2016-07-22 07:56:25 +00:00
2019-08-28 13:09:10 +00:00
![Protected branches list ](img/protected_branches_list_v12_3.png )
2016-07-22 07:56:25 +00:00
2016-08-21 07:59:19 +00:00
## Using the Allowed to merge and Allowed to push settings
2020-10-15 00:08:42 +00:00
In GitLab 8.11 and later, we added another layer of branch protection which provides
2021-01-29 15:09:40 +00:00
more granular management of protected branches. The **Developers can push**
option was replaced by **Allowed to push** . You can set this value to allow
or prohibit Maintainers and/or Developers to push to a protected branch.
2016-08-21 07:59:19 +00:00
2021-01-29 15:09:40 +00:00
Using the **Allowed to push** and **Allowed to merge** settings, you can control
2016-08-21 07:59:19 +00:00
the actions that different roles can perform with the protected branch.
2021-01-29 15:09:40 +00:00
For example, you could set **Allowed to push** to "No one", and **Allowed to merge**
to "Developers + Maintainers", to require everyone to submit a merge request for
2016-08-21 07:59:19 +00:00
changes going into the protected branch. This is compatible with workflows like
2019-10-27 06:06:30 +00:00
the [GitLab workflow ](../../topics/gitlab_flow.md ).
2016-08-21 07:59:19 +00:00
However, there are workflows where that is not needed, and only protecting from
force pushes and branch removal is useful. For those workflows, you can allow
everyone with write access to push to a protected branch by setting
2021-01-29 15:09:40 +00:00
**Allowed to push** to "Developers + Maintainers".
2016-08-21 07:59:19 +00:00
2021-01-29 15:09:40 +00:00
You can set the **Allowed to push** and **Allowed to merge** options while creating
2016-08-21 07:59:19 +00:00
a protected branch or afterwards by selecting the option you want from the
2021-01-29 15:09:40 +00:00
dropdown list in the **Already protected** area.
2016-07-22 07:56:25 +00:00
2019-08-28 13:09:10 +00:00
![Developers can push ](img/protected_branches_devs_can_push_v12_3.png )
2016-07-22 07:56:25 +00:00
2016-08-21 07:59:19 +00:00
If you don't choose any of those options while creating a protected branch,
2021-01-29 15:09:40 +00:00
they are set to Maintainers by default.
2016-07-22 07:56:25 +00:00
2021-02-05 15:09:28 +00:00
### Allow deploy keys to push to a protected branch
2020-12-09 21:09:43 +00:00
> - [Introduced](https://gitlab.com/gitlab-org/gitlab/-/issues/30769) in GitLab 13.7.
> - This feature is being selectively deployed in GitLab.com 13.7, and may not be available for all users.
2021-02-16 09:09:36 +00:00
> - This feature is available for all users in GitLab 13.9.
2020-12-09 21:09:43 +00:00
You can allow specific machines to access protected branches in your repository with
2021-02-05 15:09:28 +00:00
[deploy keys ](deploy_keys/index.md ). This can be useful for your CI/CD workflow,
2020-12-09 21:09:43 +00:00
for example.
Deploy keys can be selected in the **Allowed to push** dropdown when:
- Defining a protected branch.
- Updating an existing branch.
2021-01-29 15:09:40 +00:00
Select a deploy key to allow the key's owner to push to the chosen protected branch,
2020-12-09 21:09:43 +00:00
even if they aren't a member of the related project. The owner of the selected deploy
key must have at least read access to the given project.
For a deploy key to be selectable:
- It must be [enabled for your project ](deploy_keys/index.md#how-to-enable-deploy-keys ).
- It must have [write access ](deploy_keys/index.md#deploy-keys-permissions ) to your project repository.
2021-02-05 15:09:28 +00:00
Deploy keys are not available in the **Allowed to merge** dropdown.
2020-12-09 21:09:43 +00:00
2021-02-05 15:09:28 +00:00
![Deploy keys on protected branches ](img/protected_branches_deploy_keys_v13_5.png )
2020-12-09 21:09:43 +00:00
2021-02-03 15:09:24 +00:00
## Restricting push and merge access to certain users **(PREMIUM)**
2019-05-05 15:37:18 +00:00
2021-02-03 15:09:24 +00:00
With GitLab Premium you can restrict access to protected branches
2021-01-29 15:09:40 +00:00
by choosing a role (Maintainers, Developers) and certain users. From the
2019-05-05 15:37:18 +00:00
dropdown menu select the role and/or the users you want to have merge or push
access.
![Select roles and users ](img/protected_branches_select_roles_and_users.png )
2021-01-29 15:09:40 +00:00
Click **Protect** and the branch displays in the **Protected branch** list.
2019-05-05 15:37:18 +00:00
![Roles and users list ](img/protected_branches_select_roles_and_users_list.png )
2016-07-22 07:56:25 +00:00
## Wildcard protected branches
2021-01-29 15:09:40 +00:00
You can specify a wildcard protected branch, which protects all branches
2016-07-22 07:56:25 +00:00
matching the wildcard. For example:
| Wildcard Protected Branch | Matching Branches |
2018-09-06 16:52:18 +00:00
|---------------------------|--------------------------------------------------------|
2016-07-22 07:56:25 +00:00
| `*-stable` | `production-stable` , `staging-stable` |
| `production/*` | `production/app-server` , `production/load-balancer` |
| `*gitlab*` | `gitlab` , `gitlab/staging` , `master/gitlab/production` |
2021-01-29 15:09:40 +00:00
Protected branch settings, like **Developers can push** , apply to all matching
2016-07-22 07:56:25 +00:00
branches.
Two different wildcards can potentially match the same branch. For example,
`*-stable` and `production-*` would both match a `production-stable` branch.
In that case, if _any_ of these protected branches have a setting like
2021-01-29 15:09:40 +00:00
"Allowed to push", then `production-stable` also inherit this setting.
2016-07-22 07:56:25 +00:00
2021-01-29 15:09:40 +00:00
If you click on a protected branch's name, GitLab displays a list of
2016-08-21 07:59:19 +00:00
all matching branches:
2016-07-22 07:56:25 +00:00
![Protected branch matches ](img/protected_branches_matches.png )
2019-03-06 12:20:27 +00:00
## Creating a protected branch
2020-05-22 03:08:28 +00:00
> [Introduced](https://gitlab.com/gitlab-org/gitlab-foss/-/issues/53361) in GitLab 11.9.
2019-03-06 12:20:27 +00:00
When a protected branch or wildcard protected branches are set to
[**No one** is **Allowed to push** ](#using-the-allowed-to-merge-and-allowed-to-push-settings ),
2019-11-05 15:06:17 +00:00
Developers (and users with higher [permission levels ](../permissions.md )) are
allowed to create a new protected branch as long as they are
[**Allowed to merge** ](#using-the-allowed-to-merge-and-allowed-to-push-settings ).
2021-01-29 15:09:40 +00:00
This can only be done by using the UI or through the API, to avoid creating protected
branches accidentally from the command line or from a Git client application.
2019-03-06 12:20:27 +00:00
To create a new branch through the user interface:
2021-01-29 15:09:40 +00:00
1. Go to **Repository > Branches** .
2019-03-06 12:20:27 +00:00
1. Click on **New branch** .
2021-01-29 15:09:40 +00:00
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.
2019-03-06 12:20:27 +00:00
2017-05-19 09:10:08 +00:00
## Deleting a protected branch
2021-01-29 15:09:40 +00:00
From time to time, you may need to delete or clean up protected branches.
User with [Maintainer permissions ](../permissions.md ) and greater can manually delete protected
branches by using the GitLab web interface:
2017-05-19 09:10:08 +00:00
2021-01-29 15:09:40 +00:00
1. Go to **Repository > Branches** .
1. Click on the delete icon next to the branch you wish to delete.
1. To prevent accidental deletion, an additional confirmation is required.
2017-05-19 09:10:08 +00:00
2019-08-12 09:54:30 +00:00
![Delete protected branches ](img/protected_branches_delete.png )
2017-05-19 09:10:08 +00:00
2021-01-29 15:09:40 +00:00
Deleting a protected branch is allowed only by using the web interface; not from Git.
2017-05-19 09:10:08 +00:00
This means that you can't accidentally delete a protected branch from your
command line or a Git client application.
2021-03-29 15:09:30 +00:00
## Allow force push on protected branches
2021-03-11 18:09:23 +00:00
2021-03-29 15:09:30 +00:00
> - [Introduced](https://gitlab.com/gitlab-org/gitlab/-/issues/15611) in GitLab 13.10 behind a disabled feature flag.
> - It's enabled on GitLab.com.
> - It's recommended for production use.
> - For GitLab self-managed instances, GitLab administrators can opt to [disable it](#enable-or-disable-allow-force-push-on-protected-branches).
2021-03-11 18:09:23 +00:00
WARNING:
This feature might not be available to you. Check the **version history** note above for details.
You can allow force pushes to protected branches by either setting **Allow force push**
when you protect a new branch, or by configuring an already-protected branch.
To protect a new branch and enable Force push:
1. Navigate to your project's **Settings > Repository** .
1. Expand **Protected branches** , and scroll to **Protect a branch** .
![Code Owners approval - new protected branch ](img/code_owners_approval_new_protected_branch_v13_10.png )
1. Select a **Branch** or wildcard you'd like to protect.
1. Select the user levels **Allowed to merge** and **Allowed to push** .
1. To allow all users with push access to force push, toggle the **Allow force push** slider.
1. To reject code pushes that change files listed in the `CODEOWNERS` file, toggle
**Require approval from code owners** .
1. Click **Protect** .
To enable force pushes on branches already protected:
1. Navigate to your project's **Settings > Repository** .
1. Expand **Protected branches** and scroll to **Protected branch** .
![Code Owners approval - branch already protected ](img/code_owners_approval_protected_branch_v13_10.png )
1. Toggle the **Allow force push** slider for the chosen branch.
When enabled, members who are allowed to push to this branch can also force push.
2021-02-05 15:09:28 +00:00
## Protected branches approval by Code Owners **(PREMIUM)**
2019-10-17 18:08:05 +00:00
2021-01-29 15:09:40 +00:00
> [Introduced](https://gitlab.com/gitlab-org/gitlab/-/issues/13251) in GitLab Premium 12.4.
2019-10-17 18:08:05 +00:00
It is possible to require at least one approval by a
[Code Owner ](code_owners.md ) to a file changed by the
merge request. You can either set Code Owners approvals
at the time you protect a new branch, or set it to a branch
already protected, as described below.
To protect a new branch and enable Code Owner's approval:
1. Navigate to your project's **Settings > Repository** and expand **Protected branches** .
1. Scroll down to **Protect a branch** , select a **Branch** or wildcard you'd like to protect, select who's **Allowed to merge** and **Allowed to push** , and toggle the **Require approval from code owners** slider.
1. Click **Protect** .
2021-03-11 18:09:23 +00:00
![Code Owners approval - new protected branch ](img/code_owners_approval_new_protected_branch_v13_10.png )
2019-10-17 18:08:05 +00:00
To enable Code Owner's approval to branches already protected:
1. Navigate to your project's **Settings > Repository** and expand **Protected branches** .
1. Scroll down to **Protected branch** and toggle the **Code owner approval** slider for the chosen branch.
2021-03-11 18:09:23 +00:00
![Code Owners approval - branch already protected ](img/code_owners_approval_protected_branch_v13_10.png )
2019-10-17 18:08:05 +00:00
2021-01-29 15:09:40 +00:00
When enabled, all merge requests targeting these branches require approval
2019-10-17 18:08:05 +00:00
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.
2020-10-16 18:09:04 +00:00
[Introduced ](https://gitlab.com/gitlab-org/gitlab/-/issues/35097 ) in [GitLab Premium ](https://about.gitlab.com/pricing/ ) 13.5, users and groups who are allowed to push to protected branches do not require a merge request to merge their feature branches. Thus, they can skip merge request approval rules.
2017-09-07 11:23:02 +00:00
## Running pipelines on protected branches
The permission to merge or push to protected branches is used to define if a user can
run CI/CD pipelines and execute actions on jobs that are related to those branches.
2020-04-10 06:09:41 +00:00
See [Security on protected branches ](../../ci/pipelines/index.md#pipeline-security-on-protected-branches )
2017-09-07 11:23:02 +00:00
for details about the pipelines security model.
2021-03-11 18:09:23 +00:00
## Enable or disable allow force push on protected branches **(FREE SELF)**
2021-03-29 15:09:30 +00:00
Allow force push on protected branches is ready for
production use. It is deployed behind a feature flag that is **enabled by default** .
2021-03-11 18:09:23 +00:00
[GitLab administrators with access to the GitLab Rails console ](../../administration/feature_flags.md )
can enable it.
To enable it:
```ruby
Feature.enable(:allow_force_push_to_protected_branches)
```
To disable it:
```ruby
Feature.disable(:allow_force_push_to_protected_branches)
```
2016-07-22 07:56:25 +00:00
## Changelog
2021-01-29 15:09:40 +00:00
- **13.5**: [Allow Deploy keys to push to protected branches once more ](https://gitlab.com/gitlab-org/gitlab/-/issues/30769 ).
- **11.9**: [Allow protected branches to be created ](https://gitlab.com/gitlab-org/gitlab-foss/-/issues/53361 )
by Developers (and users with higher permission levels) through the API and the user interface.
2019-07-29 04:10:24 +00:00
<!-- ## 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. -->