Use this API to manage Git branches.

To change the branch protections configured for a project, use the protected branches API.

List all repository branches

GET /api/v4/projects/{id}/repository/branches

Lists all repository branches from a specified project, sorted alphabetically by name. Search by name, or use regular expressions to find specific branch patterns. Returns detailed information about the branch, including its protection status, merge status, and commit details. This endpoint can be accessed without authentication if the repository is publicly accessible.

Parameters

NameTypeDescription
id
Path, required
String or integerID or URL-encoded path of the project
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
StringReturn branches containing the search string. Use ^term to find branches that begin with term, and term$ to find branches that end with term
regex
Query
StringReturn branches with names matching an RE2 regular expression. Cannot be used together with search
sort
Query
StringSort results by the specified field and direction
Allowed values: name_asc, updated_asc, updated_desc
page_token
Query
StringName of the branch to start the pagination from. Used for keyset pagination

Responses

CodeDescriptionSchema
200OKArray of APIEntitiesBranch
400Bad Request—
404404 Project Not Found—

Create a repository branch

POST /api/v4/projects/{id}/repository/branches

Creates a branch in the repository.

Parameters

NameTypeDescription
id
Path, required
String or integerID or URL-encoded path of the project

Request body (application/json)

PropertyTypeDescription
branch
Required
StringName of the branch
ref
Required
StringBranch name or commit SHA to create the branch from

Responses

CodeDescriptionSchema
201CreatedAPIEntitiesBranch
400Branch already exists—
404Not Found—

Retrieve a repository branch

GET /api/v4/projects/{id}/repository/branches/{branch}

Retrieves a specified project repository branch. This endpoint can be accessed without authentication if the repository is publicly accessible.

Parameters

NameTypeDescription
id
Path, required
String or integerID or URL-encoded path of the project
branch
Path, required
StringURL-encoded name of the branch

Responses

CodeDescriptionSchema
200OKAPIEntitiesBranch
400Bad Request—
404Project Not Found—

Delete a repository branch

DELETE /api/v4/projects/{id}/repository/branches/{branch}

Deletes a specified branch from the repository. If an error occurs, the response includes a message that explains it. Deleting a branch does not completely erase all related data. Some information persists to maintain project history and to support recovery processes.

Parameters

NameTypeDescription
id
Path, required
String or integerID or URL-encoded path of the project
branch
Path, required
StringURL-encoded name of the branch. Cannot be the default branch. Only users with the Maintainer or Owner role can delete a protected branch

Responses

CodeDescriptionSchema
204No Content—
400Bad Request—
404Branch Not Found—

Check if a branch exists

HEAD /api/v4/projects/{id}/repository/branches/{branch}

Checks whether a branch exists in the repository, without returning the branch.

Parameters

NameTypeDescription
id
Path, required
String or integerID or URL-encoded path of the project
branch
Path, required
StringURL-encoded name of the branch

Responses

CodeDescriptionSchema
204No Content—
400Bad Request—
404Not Found—

Protect a single branch

PUT /api/v4/projects/{id}/repository/branches/{branch}/protect

Protects a single branch, or updates its protection if it is already protected. Only users with the Maintainer role can push or merge unless developers_can_push or developers_can_merge is true.

Parameters

NameTypeDescription
id
Path, required
String or integerID or URL-encoded path of the project
branch
Path, required
StringName of the branch

Request body (application/json)

PropertyTypeDescription
developers_can_mergeBooleanIf true, allows developers to merge to the branch
developers_can_pushBooleanIf true, allows developers to push to the branch

Responses

CodeDescriptionSchema
200OKAPIEntitiesBranch
400Bad Request—
404404 Branch Not Found—

Unprotect a single branch

PUT /api/v4/projects/{id}/repository/branches/{branch}/unprotect

Removes the protection from a single protected branch.

Parameters

NameTypeDescription
id
Path, required
String or integerID or URL-encoded path of the project
branch
Path, required
StringName of the branch

Responses

CodeDescriptionSchema
200OKAPIEntitiesBranch
400Bad Request—
404404 Branch Not Found—

Delete all merged branches

DELETE /api/v4/projects/{id}/repository/merged_branches

Deletes all branches that are merged into the default branch for a project. Protected branches are not deleted.

Parameters

NameTypeDescription
id
Path, required
String or integerID or URL-encoded path of the project

Responses

CodeDescriptionSchema
202202 Accepted—
400Bad Request—
404404 Project Not Found—
429429 Too Many Requests—

Schemas

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

APIEntitiesBranch

PropertyTypeDescription
can_pushBooleanWhether the authenticated user can push to the branch
Example: true
commitAPIEntitiesCommitLatest commit on the branch
defaultBooleanWhether the branch is the default branch of the project
Example: true
developers_can_mergeBooleanWhether a protected branch rule allows the Developer role to merge to the branch
Example: true
developers_can_pushBooleanWhether a protected branch rule allows the Developer role to push to the branch
Example: true
mergedBooleanWhether the branch is merged into the default branch
Example: true
nameStringName of the branch
Example: master
protectedBooleanWhether the branch is protected
Example: true
web_urlStringURL of the branch in the GitLab UI
Example: https://gitlab.example.com/Commit921/the-dude/-/tree/master

APIEntitiesCommit

PropertyTypeDescription
author_emailStringEmail address of the commit author
Example: john@example.com
author_nameStringName of the commit author
Example: John Smith
authored_dateString (date-time)Date and time when the commit was authored
Example: 2012-05-28T04:42:42-07:00
committed_dateString (date-time)Date and time when the commit was committed
Example: 2012-05-28T04:42:42-07:00
committer_emailStringEmail address of the committer
Example: jack@example.com
committer_nameStringName of the committer
Example: Jack Smith
created_atString (date-time)Date and time when the commit was created. Identical to committed_date
Example: 2017-07-26T11:08:53.000+02:00
extended_trailersObjectGit trailers parsed from the commit message, with every value for each key
Example: {"Signed-off-by":["John Doe \u003cjohndoe@gitlab.com\u003e","Jane Doe \u003cjanedoe@gitlab.com\u003e"]}
idStringFull SHA of the commit
Example: 2695effb5807a22ff3d138d593fd856244e155e7
messageStringFull commit message
Example: Initial commit
parent_idsArray of stringsSHAs of the parent commits
Example: ["2a4b78934375d7f53875269ffd4f45fd83a84ebe"]
short_idStringShort SHA of the commit
Example: 2695effb
titleStringTitle of the commit message
Example: Initial commit
trailersObjectGit trailers parsed from the commit message. Duplicate keys include the last value only
Example: {"Merged-By":"Jane Doe janedoe@gitlab.com"}
web_urlStringURL to view the commit in the GitLab UI
Example: https://gitlab.example.com/janedoe/gitlab-foss/-/commit/ed899a2f4b50b4370feeea94676502b42383c746