Use this API to manage protected branches for a project, and the protected branch settings that all projects in a group inherit.
GitLab Premium and GitLab Ultimate support more granular protections for pushing to branches. Administrators can grant permission to modify and push to protected branches only to deploy keys, instead of specific users.
Group protected branch settings are restricted to top-level groups. They support only valid access levels, and cannot name individual users or groups.
List all protected branches
GET /api/v4/projects/{id}/protected_branches
Lists all protected branches for a specified project. If a wildcard is set, it is returned instead of the exact name of the branches that match that wildcard.
Parameters
| Name | Type | Description |
|---|---|---|
idPath, | String or integer | ID or URL- Example: gitlab- |
pageQuery | Integer | Page number of results to retrieve Default: 1Example: 1 |
per_Query | Integer | Number of items to list per page Default: 20Example: 20 |
searchQuery | String | Name or part of the name of protected branches to search for Example: mai |
Responses
| Code | Description | Schema |
|---|---|---|
200 | OK | Array of APIEntities |
400 | Bad Request | — |
401 | 401 Unauthorized | — |
404 | 404 Project Not Found | — |
Protect repository branches
POST /api/v4/projects/{id}/protected_branches
Protects a specified repository branch or several project repository branches using a wildcard protected branch. You can set multiple access levels for allowed_to_push and allowed_to_merge. The most permissive access level determines who can perform the action. Do not include id in the allowed_to_push, allowed_to_merge, or allowed_to_unprotect arrays. An id that does not match an existing record returns 404 Not Found. Unlike the UI, the API does not clear other access levels when an element sets access_level to 0 (No access).
Parameters
| Name | Type | Description |
|---|---|---|
idPath, | String or integer | ID or URL- Example: gitlab- |
Request body (application/json)
| Property | Type | Description |
|---|---|---|
allow_ | Boolean | If true,Default: false |
allowed_ | Array of objects | Array of merge access levels,{user_,{group_,{access_ |
allowed_ | Array of objects | Array of push access levels,{user_,{group_,{deploy_,{access_ |
code_ | Boolean | Whether this branch rule requires approval from code owners.false on creation when the project can edit approval settings. |
merge_ | Integer | Access level allowed to merge.40 (Maintainer role)Allowed values: 30,40,60,0 |
nameRequired | String | Name of the branch or wildcard Example: main |
push_ | Integer | Access level allowed to push.40 (Maintainer role)Allowed values: 30,40,60,0 |
Responses
| Code | Description | Schema |
|---|---|---|
201 | Created | APIEntities |
400 | Bad Request | — |
401 | 401 Unauthorized | — |
404 | 404 Project Not Found | — |
409 | Protected branch ‘main’ already exists | — |
422 | name is missing | — |
Retrieve a protected branch or wildcard protected branch
GET /api/v4/projects/{id}/protected_branches/{name}
Retrieves a specified protected branch or wildcard protected branch.
Parameters
| Name | Type | Description |
|---|---|---|
idPath, | String or integer | ID or URL- Example: gitlab- |
namePath, | String | Name of the branch or wildcard Example: main |
Responses
| Code | Description | Schema |
|---|---|---|
200 | OK | APIEntities |
400 | Bad Request | — |
401 | 401 Unauthorized | — |
404 | 404 Project Not Found | — |
Update a protected branch
PATCH /api/v4/projects/{id}/protected_branches/{name}
Updates a protected branch for a specified project. Each element of allowed_to_push, allowed_to_merge, and allowed_to_unprotect takes the form {user_id: integer}, {group_id: integer}, {member_role_id: integer}, or {access_level: integer}. allowed_to_push also accepts {deploy_key_id: integer}. To update an existing access level record, include its id in the hash. To delete one, include its id and set _destroy to true. A user must have access to the project, and a group must have the project shared with it. A deploy key must be enabled for the project and have write access to the project repository.
Parameters
| Name | Type | Description |
|---|---|---|
idPath, | String or integer | ID or URL- Example: gitlab- |
namePath, | String | Name of the branch or wildcard Example: main |
Request body (application/json)
| Property | Type | Description |
|---|---|---|
allow_ | Boolean | If true, |
allowed_ | Array of objects | Array of merge access levels,{user_,{group_,{access_ |
allowed_ | Array of objects | Array of push access levels,{user_,{group_,{deploy_,{access_ |
code_ | Boolean | Whether this branch rule requires approval from code owners.false on creation when the project can edit approval settings. |
Responses
| Code | Description | Schema |
|---|---|---|
200 | OK | APIEntities |
400 | 400 Bad request | — |
401 | 401 Unauthorized | — |
404 | 404 Project Not Found | — |
422 | Push access levels access level has already been taken | — |
Unprotect repository branches
DELETE /api/v4/projects/{id}/protected_branches/{name}
Unprotects a specified protected branch or wildcard protected branch.
Parameters
| Name | Type | Description |
|---|---|---|
idPath, | String or integer | ID or URL- Example: gitlab- |
namePath, | String | Name of the branch or wildcard Example: main |
Responses
| Code | Description | Schema |
|---|---|---|
204 | No Content | — |
400 | Bad Request | — |
401 | 401 Unauthorized | — |
404 | 404 Project Not Found | — |
Schemas
Objects returned by the operations above and objects nested in their request bodies.
APIEntitiesProtectedBranch
| Property | Type | Description |
|---|---|---|
allow_ | Boolean | If true, |
code_ | Boolean | — |
id | Integer (int64) | ID of the protected branch Example: 1 |
merge_ | Array of APIEntities | Array of merge access level configurations |
name | String | Name of the protected branch Example: main |
push_ | Array of APIEntities | Array of push access level configurations |
APIEntitiesProtectedRefAccess
| Property | Type | Description |
|---|---|---|
access_ | Integer | Example:40 |
access_ | String | Human- Example: Maintainers |
deploy_ | Integer (int64) | ID of the deploy key with access Example: 1 |
group_ | Integer | Example:1 |
id | Integer (int64) | ID of the access level configuration Example: 1 |
user_ | Integer | Example:1 |