2019-10-12 21:52:04 +05:30
---
type: reference, howto
---
2016-08-24 12:49:21 +05:30
# Protected Branches
[Permissions ](../permissions.md ) in GitLab 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
2016-09-13 17:45:13 +05:30
## Overview
2016-08-24 12:49:21 +05:30
By default, a protected branch does four simple things:
2019-10-12 21:52:04 +05:30
- It prevents its creation, if not already created, from everybody except users
with Maintainer permission.
2019-12-04 20:38:33 +05:30
- It prevents pushes from everybody except users with **Allowed** permission.
2019-10-12 21:52:04 +05:30
- It prevents **anyone** from force pushing to the branch.
- It prevents **anyone** from deleting the branch.
2016-08-24 12:49:21 +05:30
2019-10-12 21:52:04 +05:30
NOTE: **Note:**
2019-07-31 22:56:46 +05:30
A GitLab admin is allowed to push to the protected branches.
2016-08-24 12:49:21 +05:30
2019-07-31 22:56:46 +05:30
See the [Changelog ](#changelog ) section for changes over time.
2016-09-13 17:45:13 +05:30
2019-12-21 20:55:43 +05:30
The default branch protection level is set in the [Admin Area ](../admin_area/settings/visibility_and_access_controls.md#default-branch-protection ).
2016-08-24 12:49:21 +05:30
## Configuring protected branches
2018-11-08 19:23:39 +05:30
To protect a branch, you need to have at least Maintainer permission level. Note
2016-08-24 12:49:21 +05:30
that the `master` branch is protected by default.
2017-09-10 17:25:29 +05:30
1. Navigate to your project's **Settings ➔ Repository**
1. Scroll to find the **Protected branches** section.
2016-08-24 12:49:21 +05:30
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-12-04 20:38:33 +05:30
![Protected branches page ](img/protected_branches_page_v12_3.png )
2016-08-24 12:49:21 +05:30
2016-09-13 17:45:13 +05:30
1. Once done, the protected branch will appear in the "Protected branches" list.
2016-08-24 12:49:21 +05:30
2019-12-04 20:38:33 +05:30
![Protected branches list ](img/protected_branches_list_v12_3.png )
2016-08-24 12:49:21 +05:30
2016-09-13 17:45:13 +05:30
## Using the Allowed to merge and Allowed to push settings
2020-03-13 15:44:24 +05:30
> [Introduced](https://gitlab.com/gitlab-org/gitlab-foss/-/merge_requests/5081) in GitLab 8.11.
2016-09-13 17:45:13 +05:30
Since GitLab 8.11, we added another layer of branch protection which provides
more granular management of protected branches. The "Developers can push"
option was replaced by an "Allowed to push" setting which can be set to
2018-11-08 19:23:39 +05:30
allow/prohibit Maintainers and/or Developers to push to a protected branch.
2016-09-13 17:45:13 +05:30
Using the "Allowed to push" and "Allowed to merge" settings, you can control
the actions that different roles can perform with the protected branch.
For example, you could set "Allowed to push" to "No one", and "Allowed to merge"
2018-11-08 19:23:39 +05:30
to "Developers + Maintainers", to require _everyone_ to submit a merge request for
2016-09-13 17:45:13 +05:30
changes going into the protected branch. This is compatible with workflows like
2019-12-26 22:10:19 +05:30
the [GitLab workflow ](../../topics/gitlab_flow.md ).
2016-09-13 17:45:13 +05:30
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
2018-11-08 19:23:39 +05:30
"Allowed to push" to "Developers + Maintainers".
2016-09-13 17:45:13 +05:30
You can set the "Allowed to push" and "Allowed to merge" options while creating
a protected branch or afterwards by selecting the option you want from the
dropdown list in the "Already protected" area.
2016-08-24 12:49:21 +05:30
2019-12-04 20:38:33 +05:30
![Developers can push ](img/protected_branches_devs_can_push_v12_3.png )
2016-08-24 12:49:21 +05:30
2016-09-13 17:45:13 +05:30
If you don't choose any of those options while creating a protected branch,
2018-11-08 19:23:39 +05:30
they are set to "Maintainers" by default.
2016-08-24 12:49:21 +05:30
2019-09-30 21:07:59 +05:30
## Restricting push and merge access to certain users **(STARTER)**
2019-07-31 22:56:46 +05:30
2020-03-13 15:44:24 +05:30
> [Introduced](https://gitlab.com/gitlab-org/gitlab-foss/-/merge_requests/5081) in [GitLab Starter](https://about.gitlab.com/pricing/) 8.11.
2019-07-31 22:56:46 +05:30
With GitLab Enterprise Edition you can restrict access to protected branches
by choosing a role (Maintainers, Developers) as well as certain users. From the
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 )
Click **Protect** and the branch will appear in the "Protected branch" list.
![Roles and users list ](img/protected_branches_select_roles_and_users_list.png )
2016-08-24 12:49:21 +05:30
## Wildcard protected branches
2020-03-13 15:44:24 +05:30
> [Introduced](https://gitlab.com/gitlab-org/gitlab-foss/-/merge_requests/4665) in GitLab 8.10.
2016-08-24 12:49:21 +05:30
You can specify a wildcard protected branch, which will protect all branches
matching the wildcard. For example:
| Wildcard Protected Branch | Matching Branches |
2018-11-20 20:47:30 +05:30
|---------------------------|--------------------------------------------------------|
2016-08-24 12:49:21 +05:30
| `*-stable` | `production-stable` , `staging-stable` |
| `production/*` | `production/app-server` , `production/load-balancer` |
| `*gitlab*` | `gitlab` , `gitlab/staging` , `master/gitlab/production` |
Protected branch settings (like "Developers can push") apply to all matching
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
"Allowed to push", then `production-stable` will also inherit this setting.
2016-09-13 17:45:13 +05:30
If you click on a protected branch's name, you will be presented with a list of
all matching branches:
2016-08-24 12:49:21 +05:30
![Protected branch matches ](img/protected_branches_matches.png )
2019-07-07 11:18:12 +05:30
## Creating a protected branch
2019-12-04 20:38:33 +05:30
> [Introduced](https://gitlab.com/gitlab-org/gitlab-foss/issues/53361) in GitLab 11.9.
2019-07-07 11:18:12 +05:30
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-12-26 22:10:19 +05:30
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 ).
This can only be done via the UI or through the API (to avoid creating protected
branches accidentally from the command line or from a Git client application).
2019-07-07 11:18:12 +05:30
To create a new branch through the user interface:
1. Visit **Repository > Branches** .
1. Click on **New branch** .
1. Fill in the branch name and select an existing branch, tag, or commit that
the new branch will be based off. Only existing protected branches and commits
that are already in protected branches will be accepted.
2017-09-10 17:25:29 +05:30
## Deleting a protected branch
2019-12-21 20:55:43 +05:30
> [Introduced](https://gitlab.com/gitlab-org/gitlab-foss/issues/21393) in GitLab 9.3.
2017-09-10 17:25:29 +05:30
From time to time, it may be required to delete or clean up branches that are
protected.
2019-12-21 20:55:43 +05:30
User with [Maintainer permissions ](../permissions.md ) and up can manually delete protected
2017-09-10 17:25:29 +05:30
branches via GitLab's web interface:
1. Visit **Repository > Branches**
1. Click on the delete icon next to the branch you wish to delete
1. In order to prevent accidental deletion, an additional confirmation is
required
2019-10-12 21:52:04 +05:30
![Delete protected branches ](img/protected_branches_delete.png )
2017-09-10 17:25:29 +05:30
Deleting a protected branch is only allowed via the web interface, not via Git.
This means that you can't accidentally delete a protected branch from your
command line or a Git client application.
2019-12-21 20:55:43 +05:30
## Protected Branches approval by Code Owners **(PREMIUM)**
> [Introduced](https://gitlab.com/gitlab-org/gitlab/issues/13251) in [GitLab Premium](https://about.gitlab.com/pricing/) 12.4.
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** .
![Code Owners approval - new protected branch ](img/code_owners_approval_new_protected_branch_v12_4.png )
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.
![Code Owners approval - branch already protected ](img/code_owners_approval_protected_branch_v12_4.png )
When enabled, all merge requests targeting these branches will require approval
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.
2018-03-17 18:26:18 +05:30
## 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-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.
2016-09-13 17:45:13 +05:30
## Changelog
2016-08-24 12:49:21 +05:30
2019-07-07 11:18:12 +05:30
**11.9**
2019-12-04 20:38:33 +05:30
- [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-07 11:18:12 +05:30
2017-09-10 17:25:29 +05:30
**9.2**
2019-12-21 20:55:43 +05:30
- Allow deletion of protected branches via the web interface ([issue #21393 ](https://gitlab.com/gitlab-org/gitlab-foss/issues/21393)).
2017-09-10 17:25:29 +05:30
2016-09-13 17:45:13 +05:30
**8.11**
2016-08-24 12:49:21 +05:30
2020-03-13 15:44:24 +05:30
- Allow creating protected branches that can't be pushed to ([merge request !5081](https://gitlab.com/gitlab-org/gitlab-foss/-/merge_requests/5081)).
2016-08-24 12:49:21 +05:30
2016-09-13 17:45:13 +05:30
**8.10**
2016-08-24 12:49:21 +05:30
2020-03-13 15:44:24 +05:30
- Allow developers without push access to merge into a protected branch ([merge request !4892](https://gitlab.com/gitlab-org/gitlab-foss/-/merge_requests/4892)).
- Allow specifying protected branches using wildcards ([merge request !4665](https://gitlab.com/gitlab-org/gitlab-foss/-/merge_requests/4665)).
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. -->