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

NameTypeDescription
id
Path, required
String or integerID or URL-encoded path of the project
Example: gitlab-org/gitlab
page
Query
IntegerPage number of results to retrieve
Default: 1
Example: 1
per_page
Query
IntegerNumber of items to list per page
Default: 20
Example: 20
search
Query
StringName or part of the name of protected branches to search for
Example: mai

Responses

CodeDescriptionSchema
200OKArray of APIEntitiesProtectedBranch
400Bad Request—
401401 Unauthorized—
404404 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

NameTypeDescription
id
Path, required
String or integerID or URL-encoded path of the project
Example: gitlab-org/gitlab

Request body (application/json)

PropertyTypeDescription
allow_force_pushBooleanIf true, members who can push to this branch can also force push
Default: false
allowed_to_mergeArray of objectsArray of merge access levels, with each described by a hash of the form {user_id: integer}, {group_id: integer}, or {access_level: integer}
allowed_to_pushArray of objectsArray of push access levels, with each described by a hash of the form {user_id: integer}, {group_id: integer}, {deploy_key_id: integer}, or {access_level: integer}
code_owner_approval_requiredBooleanWhether this branch rule requires approval from code owners. Defaults to false on creation when the project can edit approval settings. Leave out on update to keep the current value
merge_access_levelIntegerAccess level allowed to merge. Defaults to 40 (Maintainer role)
Allowed values: 30, 40, 60, 0
name
Required
StringName of the branch or wildcard
Example: main
push_access_levelIntegerAccess level allowed to push. Defaults to 40 (Maintainer role)
Allowed values: 30, 40, 60, 0

Responses

CodeDescriptionSchema
201CreatedAPIEntitiesProtectedBranch
400Bad Request—
401401 Unauthorized—
404404 Project Not Found—
409Protected branch ‘main’ already exists—
422name 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

NameTypeDescription
id
Path, required
String or integerID or URL-encoded path of the project
Example: gitlab-org/gitlab
name
Path, required
StringName of the branch or wildcard
Example: main

Responses

CodeDescriptionSchema
200OKAPIEntitiesProtectedBranch
400Bad Request—
401401 Unauthorized—
404404 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

NameTypeDescription
id
Path, required
String or integerID or URL-encoded path of the project
Example: gitlab-org/gitlab
name
Path, required
StringName of the branch or wildcard
Example: main

Request body (application/json)

PropertyTypeDescription
allow_force_pushBooleanIf true, members who can push to this branch can also force push
allowed_to_mergeArray of objectsArray of merge access levels, with each described by a hash of the form {user_id: integer}, {group_id: integer}, or {access_level: integer}
allowed_to_pushArray of objectsArray of push access levels, with each described by a hash of the form {user_id: integer}, {group_id: integer}, {deploy_key_id: integer}, or {access_level: integer}
code_owner_approval_requiredBooleanWhether this branch rule requires approval from code owners. Defaults to false on creation when the project can edit approval settings. Leave out on update to keep the current value

Responses

CodeDescriptionSchema
200OKAPIEntitiesProtectedBranch
400400 Bad request—
401401 Unauthorized—
404404 Project Not Found—
422Push 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

NameTypeDescription
id
Path, required
String or integerID or URL-encoded path of the project
Example: gitlab-org/gitlab
name
Path, required
StringName of the branch or wildcard
Example: main

Responses

CodeDescriptionSchema
204No Content—
400Bad Request—
401401 Unauthorized—
404404 Project Not Found—

Schemas

Objects returned by the operations above and objects nested in their request bodies.

APIEntitiesProtectedBranch

PropertyTypeDescription
allow_force_pushBooleanIf true, force push is allowed on the branch
code_owner_approval_requiredBoolean—
idInteger (int64)ID of the protected branch
Example: 1
merge_access_levelsArray of APIEntitiesProtectedRefAccessArray of merge access level configurations
nameStringName of the protected branch
Example: main
push_access_levelsArray of APIEntitiesProtectedRefAccessArray of push access level configurations

APIEntitiesProtectedRefAccess

PropertyTypeDescription
access_levelIntegerExample: 40
access_level_descriptionStringHuman-readable description of the access level
Example: Maintainers
deploy_key_idInteger (int64)ID of the deploy key with access
Example: 1
group_idIntegerExample: 1
idInteger (int64)ID of the access level configuration
Example: 1
user_idIntegerExample: 1