Branches API

  • Tier: Free, Premium, Ultimate
  • Offering: GitLab.com, GitLab Self-Managed, GitLab Dedicated

Use the branches API to manage a project’s Git branches programmatically.

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

List repository branches

Get a list of repository branches from a project, sorted by name alphabetically. 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.

GET /projects/:id/repository/branches

Supported attributes:

AttributeTypeRequiredDescription
idinteger or stringYesID or URL-encoded path of the project.
regexstringNoReturn list of branches with names matching a re2 regular expression. Cannot be used together with search.
searchstringNoReturn list of branches containing the search string. You can use ^term to find branches that begin with term, and term$ to find branches that end with term.

If successful, returns 200 OK and the following response attributes:

AttributeTypeDescription
can_pushbooleanIf true, the authenticated user can push to this branch.
commitobjectDetails about the most recent commit on the branch.
commit.author_emailstringEmail address of the user who authored the change.
commit.author_namestringName of the user who authored the change.
commit.authored_datedatetime (ISO 8601)When the commit was authored.
commit.committed_datedatetime (ISO 8601)When the commit was committed.
commit.committer_emailstringEmail address of the user who committed the change.
commit.committer_namestringName of the user who committed the change.
commit.created_atdatetime (ISO 8601)When the commit was created.
commit.extended_trailersobjectExtended Git trailers parsed from the commit message.
commit.idstringFull SHA of the commit.
commit.messagestringFull commit message.
commit.parent_idsarrayArray of parent commit SHAs.
commit.short_idstringAbbreviated SHA of the commit.
commit.titlestringTitle of the commit message.
commit.trailersobjectGit trailers parsed from the commit message.
commit.web_urlstringURL to view the commit in the GitLab UI.
defaultbooleanIf true, the branch is the default branch for the project.
developers_can_mergebooleanIf true, users with at least the Developer role can merge to this branch.
developers_can_pushbooleanIf true, users with at least the Developer role can push to this branch.
mergedbooleanIf true, the branch has been merged into the default branch.
namestringName of the branch.
protectedbooleanIf true, the branch is protected from force pushes and deletion.
web_urlstringURL to view the branch in the GitLab UI.

Example request:

curl --header "PRIVATE-TOKEN: <your_access_token>" \
  --url "https://gitlab.example.com/api/v4/projects/5/repository/branches"

Example response:

[
  {
    "name": "main",
    "merged": false,
    "protected": true,
    "default": true,
    "developers_can_push": false,
    "developers_can_merge": false,
    "can_push": true,
    "web_url": "https://gitlab.example.com/my-group/my-project/-/tree/main",
    "commit": {
      "id": "7b5c3cc8be40ee161ae89a06bba6229da1032a0c",
      "short_id": "7b5c3cc",
      "created_at": "2024-06-28T03:44:20-07:00",
      "parent_ids": [
        "4ad91d3c1144c406e50c7b33bae684bd6837faf8"
      ],
      "title": "add projects API",
      "message": "add projects API",
      "author_name": "John Smith",
      "author_email": "john@example.com",
      "authored_date": "2024-06-27T05:51:39-07:00",
      "committer_name": "John Smith",
      "committer_email": "john@example.com",
      "committed_date": "2024-06-28T03:44:20-07:00",
      "trailers": {},
      "extended_trailers": {},
      "web_url": "https://gitlab.example.com/my-group/my-project/-/commit/7b5c3cc8be40ee161ae89a06bba6229da1032a0c"
    }
  },
  ...
]

Get single repository branch

Get a single project repository branch.

This endpoint can be accessed without authentication if the repository is publicly accessible.

GET /projects/:id/repository/branches/:branch

Supported attributes:

AttributeTypeRequiredDescription
idinteger or stringYesID or URL-encoded path of the project.
branchstringYesURL-encoded name of the branch.

If successful, returns 200 OK and the following response attributes:

AttributeTypeDescription
can_pushbooleanWhether the authenticated user can push to this branch.
commitobjectDetails about the latest commit on the branch.
commit.author_emailstringEmail address of the commit author.
commit.author_namestringName of the commit author.
commit.authored_datestringDate and time when the commit was authored, in ISO 8601 format.
commit.committer_emailstringEmail address of the user who committed the change.
commit.committer_namestringName of the user who committed the change.
commit.committed_datestringDate and time when the commit was committed, in ISO 8601 format.
commit.created_atstringDate and time when the commit was created, in ISO 8601 format.
commit.extended_trailersobjectExtended Git trailers parsed from the commit message.
commit.idstringFull SHA of the commit.
commit.messagestringFull commit message.
commit.parent_idsarrayArray of parent commit SHAs.
commit.short_idstringAbbreviated SHA of the commit.
commit.titlestringTitle of the commit message.
commit.trailersobjectGit trailers parsed from the commit message.
commit.web_urlstringURL to view the commit in the GitLab UI.
defaultbooleanWhether this is the default branch for the project.
developers_can_mergebooleanWhether users with the Developer role can merge to this branch.
developers_can_pushbooleanWhether users with the Developer role can push to this branch.
mergedbooleanWhether the branch has been merged into the default branch.
namestringName of the branch.
protectedbooleanWhether the branch is protected from force pushes and deletion.
web_urlstringURL to view the branch in the GitLab UI.

Example request:

curl --header "PRIVATE-TOKEN: <your_access_token>" \
  --url "https://gitlab.example.com/api/v4/projects/5/repository/branches/main"

Example response:

{
  "name": "main",
  "merged": false,
  "protected": true,
  "default": true,
  "developers_can_push": false,
  "developers_can_merge": false,
  "can_push": true,
  "web_url": "https://gitlab.example.com/my-group/my-project/-/tree/main",
  "commit": {
    "id": "7b5c3cc8be40ee161ae89a06bba6229da1032a0c",
    "short_id": "7b5c3cc",
    "created_at": "2012-06-28T03:44:20-07:00",
    "parent_ids": [
      "4ad91d3c1144c406e50c7b33bae684bd6837faf8"
    ],
    "title": "add projects API",
    "message": "add projects API",
    "author_name": "John Smith",
    "author_email": "john@example.com",
    "authored_date": "2012-06-27T05:51:39-07:00",
    "committer_name": "John Smith",
    "committer_email": "john@example.com",
    "committed_date": "2012-06-28T03:44:20-07:00",
    "trailers": {},
    "extended_trailers": {},
    "web_url": "https://gitlab.example.com/my-group/my-project/-/commit/7b5c3cc8be40ee161ae89a06bba6229da1032a0c"
  }
}

Protect repository branch

See POST /projects/:id/protected_branches for information on protecting repository branches.

Unprotect repository branch

See DELETE /projects/:id/protected_branches/:name for information on unprotecting repository branches.

Create repository branch

Create a new branch in the repository.

POST /projects/:id/repository/branches

Supported attributes:

AttributeTypeRequiredDescription
idinteger or stringYesID or URL-encoded path of the project.
branchstringYesName of the branch. Cannot contain spaces or special characters except hyphens and underscores.
refstringYesBranch name or commit SHA to create the branch from.

If successful, returns 201 Created and the following response attributes:

AttributeTypeDescription
can_pushbooleanIf true, the authenticated user can push to this branch.
commitobjectDetails about the latest commit on the branch.
commit.author_emailstringEmail address of the commit author.
commit.author_namestringName of the commit author.
commit.authored_datestringDate and time when the commit was authored, in ISO 8601 format.
commit.committed_datestringDate and time when the commit was committed, in ISO 8601 format.
commit.committer_emailstringEmail address of the user who committed the change.
commit.committer_namestringName of the user who committed the change.
commit.created_atstringDate and time when the commit was created, in ISO 8601 format.
commit.extended_trailersobjectExtended Git trailers parsed from the commit message.
commit.idstringFull SHA of the commit.
commit.messagestringFull commit message.
commit.parent_idsarrayArray of parent commit SHAs.
commit.short_idstringAbbreviated SHA of the commit.
commit.titlestringTitle of the commit message.
commit.trailersobjectGit trailers parsed from the commit message.
commit.web_urlstringURL to view the commit in the GitLab UI.
defaultbooleanIf true, sets this branch is the default branch for the project.
developers_can_mergebooleanIf true, users with the Developer role can merge to this branch.
developers_can_pushbooleanIf true, users with the Developer role can push to this branch.
mergedbooleanIf true, the branch merged into the default branch.
namestringName of the branch.
protectedbooleanIf true, the branch is protected from force pushes and deletion.
web_urlstringURL to view the branch in the GitLab UI.

Example request:

curl --request POST \
  --header "PRIVATE-TOKEN: <your_access_token>" \
  --url "https://gitlab.example.com/api/v4/projects/5/repository/branches?branch=newbranch&ref=main"

Example response:

{
  "commit": {
    "id": "7b5c3cc8be40ee161ae89a06bba6229da1032a0c",
    "short_id": "7b5c3cc",
    "created_at": "2012-06-28T03:44:20-07:00",
    "parent_ids": [
      "4ad91d3c1144c406e50c7b33bae684bd6837faf8"
    ],
    "title": "add projects API",
    "message": "add projects API",
    "author_name": "John Smith",
    "author_email": "john@example.com",
    "authored_date": "2012-06-27T05:51:39-07:00",
    "committer_name": "John Smith",
    "committer_email": "john@example.com",
    "committed_date": "2012-06-28T03:44:20-07:00",
    "trailers": {},
    "extended_trailers": {},
    "web_url": "https://gitlab.example.com/my-group/my-project/-/commit/7b5c3cc8be40ee161ae89a06bba6229da1032a0c"
  },
  "name": "newbranch",
  "merged": false,
  "protected": false,
  "default": false,
  "developers_can_push": false,
  "developers_can_merge": false,
  "can_push": true,
  "web_url": "https://gitlab.example.com/my-group/my-project/-/tree/newbranch"
}

Delete repository branch

Delete a branch from the repository.

In the case of an error, an explanation message is provided.

DELETE /projects/:id/repository/branches/:branch

Supported attributes:

AttributeTypeRequiredDescription
idinteger or stringYesID or URL-encoded path of the project.
branchstringYesURL-encoded name of the branch. Cannot delete the default branch or protected branches.

If successful, returns 204 No Content.

Example request:

curl --request DELETE \
  --header "PRIVATE-TOKEN: <your_access_token>" \
  --url "https://gitlab.example.com/api/v4/projects/5/repository/branches/newbranch"

Deleting a branch does not completely erase all related data. Some information persists to maintain project history and to support recovery processes. For more information, see Handle sensitive information.

Delete merged branches

Deletes all branches that are merged into the project’s default branch.

Protected branches are not deleted as part of this operation.

DELETE /projects/:id/repository/merged_branches

Supported attributes:

AttributeTypeRequiredDescription
idinteger or stringYesID or URL-encoded path of the project.

If successful, returns 202 Accepted.

Example request:

curl --request DELETE \
  --header "PRIVATE-TOKEN: <your_access_token>" \
  --url "https://gitlab.example.com/api/v4/projects/5/repository/merged_branches"