2016-07-22 03:56:25 -04:00
# Protected Branches
[Permissions ](../permissions.md ) in GitLab are fundamentally defined around the
idea of having read or write permission to the repository and branches. To
prevent people from messing with history or pushing code without review, we've
created protected branches.
2016-08-21 03:59:19 -04:00
## Overview
2016-07-22 03:56:25 -04:00
By default, a protected branch does four simple things:
- it prevents its creation, if not already created, from everybody except users
2019-03-21 07:11:06 -04:00
with Maintainer permission
2018-05-22 06:16:06 -04:00
- it prevents pushes from everybody except users with Maintainer permission
2016-07-22 03:56:25 -04:00
- it prevents **anyone** from force pushing to the branch
- it prevents **anyone** from deleting the branch
2019-05-05 11:37:18 -04:00
>**Note**:
A GitLab admin is allowed to push to the protected branches.
2016-07-22 03:56:25 -04:00
2019-05-05 11:37:18 -04:00
See the [Changelog ](#changelog ) section for changes over time.
2016-08-21 03:59:19 -04:00
2016-07-22 03:56:25 -04:00
## Configuring protected branches
2018-05-22 06:16:06 -04:00
To protect a branch, you need to have at least Maintainer permission level. Note
2016-07-22 03:56:25 -04:00
that the `master` branch is protected by default.
2017-06-06 06:45:55 -04:00
1. Navigate to your project's **Settings ➔ Repository**
1. Scroll to find the **Protected branches** section.
2016-07-22 03:56:25 -04: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.
2016-08-21 03:59:19 -04:00
![Protected branches page ](img/protected_branches_page.png )
2016-07-22 03:56:25 -04:00
2016-08-21 03:59:19 -04:00
1. Once done, the protected branch will appear in the "Protected branches" list.
2016-07-22 03:56:25 -04:00
![Protected branches list ](img/protected_branches_list.png )
2016-08-21 03:59:19 -04:00
## Using the Allowed to merge and Allowed to push settings
2016-08-22 08:22:05 -04:00
> [Introduced][ce-5081] in GitLab 8.11.
2016-08-21 03:59:19 -04:00
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-05-22 06:16:06 -04:00
allow/prohibit Maintainers and/or Developers to push to a protected branch.
2016-08-21 03:59:19 -04:00
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-05-22 07:54:50 -04:00
to "Developers + Maintainers", to require _everyone_ to submit a merge request for
2016-08-21 03:59:19 -04:00
changes going into the protected branch. This is compatible with workflows like
the [GitLab workflow ](../../workflow/gitlab_flow.md ).
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-05-22 07:54:50 -04:00
"Allowed to push" to "Developers + Maintainers".
2016-08-21 03:59:19 -04:00
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-07-22 03:56:25 -04:00
2016-08-21 03:59:19 -04:00
![Developers can push ](img/protected_branches_devs_can_push.png )
2016-07-22 03:56:25 -04:00
2016-08-21 03:59:19 -04:00
If you don't choose any of those options while creating a protected branch,
2018-05-22 06:16:06 -04:00
they are set to "Maintainers" by default.
2016-07-22 03:56:25 -04:00
2019-05-05 11:37:18 -04:00
## Restricting push and merge access to certain users **[STARTER]**
> This feature was [introduced][ce-5081] in [GitLab Starter][ee] 8.11.
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-07-22 03:56:25 -04:00
## Wildcard protected branches
2016-08-22 08:22:05 -04:00
> [Introduced][ce-4665] in GitLab 8.10.
2016-07-22 03:56:25 -04:00
You can specify a wildcard protected branch, which will protect all branches
matching the wildcard. For example:
| Wildcard Protected Branch | Matching Branches |
2018-09-06 12:52:18 -04:00
|---------------------------|--------------------------------------------------------|
2016-07-22 03:56:25 -04:00
| `*-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-08-21 03:59:19 -04:00
If you click on a protected branch's name, you will be presented with a list of
all matching branches:
2016-07-22 03:56:25 -04:00
![Protected branch matches ](img/protected_branches_matches.png )
2019-03-06 07:20:27 -05:00
## Creating a protected branch
2019-03-13 07:00:35 -04:00
> [Introduced](https://gitlab.com/gitlab-org/gitlab-ce/issues/53361) in GitLab 11.9.
2019-03-06 07:20:27 -05: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 ),
Developers (and users with higher [permission levels ](../permissions.md )) are allowed
to create a new protected branch, but only via the UI or through the API (to avoid
creating protected branches accidentally from the command line or from a Git
client application).
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-05-19 05:10:08 -04:00
## Deleting a protected branch
> [Introduced][ce-21393] in GitLab 9.3.
From time to time, it may be required to delete or clean up branches that are
protected.
2018-05-22 06:16:06 -04:00
User with [Maintainer permissions][perm] and up can manually delete protected
2017-05-19 05:10:08 -04:00
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
![Delete protected branches ](img/protected_branches_delete.png )
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.
2017-09-07 07:23:02 -04: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.
See [Security on protected branches ](../../ci/pipelines.md#security-on-protected-branches )
for details about the pipelines security model.
2016-07-22 03:56:25 -04:00
## Changelog
2019-03-06 07:20:27 -05:00
**11.9**
- [Allow protected branches to be created ](https://gitlab.com/gitlab-org/gitlab-ce/issues/53361 ) by Developers (and users with higher permission levels) through the API and the user interface.
2017-05-19 05:10:08 -04:00
**9.2**
- Allow deletion of protected branches via the web interface [gitlab-org/gitlab-ce#21393][ce-21393]
2016-08-22 08:14:36 -04:00
**8.11**
- Allow creating protected branches that can't be pushed to [gitlab-org/gitlab-ce!5081][ce-5081]
2016-08-21 03:59:19 -04:00
**8.10**
2016-07-22 03:56:25 -04:00
2016-08-22 08:14:36 -04:00
- Allow developers to merge into a protected branch without having push access [gitlab-org/gitlab-ce!4892][ce-4892]
- Allow specifying protected branches using wildcards [gitlab-org/gitlab-ce!4665][ce-4665]
2016-07-22 03:56:25 -04:00
---
[ce-4665]: https://gitlab.com/gitlab-org/gitlab-ce/merge_requests/4665 "Allow specifying protected branches using wildcards"
2016-08-22 08:14:36 -04:00
[ce-4892]: https://gitlab.com/gitlab-org/gitlab-ce/merge_requests/4892 "Allow developers to merge into a protected branch without having push access"
2016-07-22 03:56:25 -04:00
[ce-5081]: https://gitlab.com/gitlab-org/gitlab-ce/merge_requests/5081 "Allow creating protected branches that can't be pushed to"
2017-05-19 05:10:08 -04:00
[ce-21393]: https://gitlab.com/gitlab-org/gitlab-ce/issues/21393
2016-08-21 03:59:19 -04:00
[ee-restrict]: http://docs.gitlab.com/ee/user/project/protected_branches.html#restricting-push-and-merge-access-to-certain-users
2017-05-19 05:10:08 -04:00
[perm]: ../permissions.md
2019-05-05 11:37:18 -04:00
[ee]: https://about.gitlab.com/pricing/