Repositories API

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

Use this API to manage your GitLab repository.

List repository tree

Get a list of repository files and directories in a project. This endpoint can be accessed without authentication if the repository is publicly accessible.

This command provides essentially the same features as the git ls-tree command. For more information, refer to the section Tree Objects in the Git internals documentation.

GitLab version 17.7 changes the error handling behavior when a requested path is not found. The endpoint now returns a status code 404 Not Found. Previously, the status code was 200 OK.

If your implementation relies on receiving a 200 status code with an empty array for missing paths, you must update your error handling to handle the new 404 responses.

GET /projects/:id/repository/tree

Supported attributes:

AttributeTypeRequiredDescription
idinteger or stringYesID or URL-encoded path of the project.
page_tokenstringNoTree record ID at which to fetch the next page. Used only with keyset pagination.
paginationstringNoIf keyset, use the keyset-based pagination method.
pathstringNoPath inside the repository. Used to get content of subdirectories.
per_pageintegerNoNumber of results to show per page. If not specified, defaults to 20. For more information, see Pagination.
recursivebooleanNoIf true, get a recursive tree. Default is false.
refstringNoName of a repository branch or tag. If not specified, uses the default branch.

If successful, returns 200 OK and an array of tree objects.

Example request:

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

Example response:

[
  {
    "id": "a1e8f8d745cc87e3a9248358d9352bb7f9a0aeba",
    "name": "html",
    "type": "tree",
    "path": "files/html",
    "mode": "040000"
  },
  {
    "id": "4535904260b1082e14f867f7a24fd8c21495bde3",
    "name": "images",
    "type": "tree",
    "path": "files/images",
    "mode": "040000"
  },
  {
    "id": "31405c5ddef582c5a9b7a85230413ff90e2fe720",
    "name": "js",
    "type": "tree",
    "path": "files/js",
    "mode": "040000"
  },
  {
    "id": "cc71111cfad871212dc99572599a568bfe1e7e00",
    "name": "lfs",
    "type": "tree",
    "path": "files/lfs",
    "mode": "040000"
  },
  {
    "id": "fd581c619bf59cfdfa9c8282377bb09c2f897520",
    "name": "markdown",
    "type": "tree",
    "path": "files/markdown",
    "mode": "040000"
  },
  {
    "id": "23ea4d11a4bdd960ee5320c5cb65b5b3fdbc60db",
    "name": "ruby",
    "type": "tree",
    "path": "files/ruby",
    "mode": "040000"
  },
  {
    "id": "7d70e02340bac451f281cecf0a980907974bd8be",
    "name": "whitespace",
    "type": "blob",
    "path": "files/whitespace",
    "mode": "100644"
  }
]

Get a blob from repository

Allows you to receive information, such as size and content, about blobs in a repository. Blob content is Base64 encoded. This endpoint can be accessed without authentication, if the repository is publicly accessible.

For blobs larger than 10 MB, this endpoint has a rate limit of 5 requests per minute.

GET /projects/:id/repository/blobs/:sha

Supported attributes:

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

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

AttributeTypeDescription
contentstringBase64 encoded blob content.
encodingstringEncoding used for the blob content.
shastringBlob SHA.
sizeintegerSize of the blob in bytes.

Example request:

curl --header "PRIVATE-TOKEN: <your_access_token>" \
  --url "https://gitlab.example.com/api/v4/projects/13083/repository/blobs/79f7bbd25901e8334750839545a9bd021f0e4c83"

Example response:

{
  "size": 1476,
  "encoding": "base64",
  "content": "VGhpcyBpcyBhIGJpbmFyeSBmaWxl",
  "sha": "79f7bbd25901e8334750839545a9bd021f0e4c83"
}

Get raw blob content

Get the raw file contents for a blob, by blob SHA. This endpoint can be accessed without authentication if the repository is publicly accessible.

GET /projects/:id/repository/blobs/:sha/raw

Supported attributes:

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

Example request:

curl --header "PRIVATE-TOKEN: <your_access_token>" \
  --url "https://gitlab.example.com/api/v4/projects/13083/repository/blobs/79f7bbd25901e8334750839545a9bd021f0e4c83/raw"

Get file archive

Get an archive of the repository. This endpoint can be accessed without authentication if the repository is publicly accessible.

For GitLab.com users, this endpoint has a rate limit threshold of 5 requests per minute.

GET /projects/:id/repository/archive[.format]

format is an optional suffix for the archive format, and defaults to tar.gz. For example, specifying archive.zip sends an archive in ZIP format. Available options are:

  • bz2
  • tar
  • tar.bz2
  • tar.gz
  • tb2
  • tbz
  • tbz2
  • zip

Supported attributes:

AttributeTypeRequiredDescription
idinteger or stringYesID or URL-encoded path of the project.
exclude_pathsstringNoComma-separated list of paths to exclude from the archive.
include_lfs_blobsbooleanNoIf true, LFS objects are included in the archive. When set to false, LFS objects are excluded. Default is true.
pathstringNoSubpath of the repository to download. If an empty string, defaults to the whole repository.
shastringNoCommit SHA to download. Accepts a tag, branch reference, or SHA. If not specified, defaults to the tip of the default branch.

Example request:

curl --header "PRIVATE-TOKEN: <your_access_token>" \
  --url "https://gitlab.com/api/v4/projects/<project_id>/repository/archive?sha=<commit_sha>&path=<path>&exclude_paths=<path1,path2>"

Compare branches, tags, or commits

This endpoint can be accessed without authentication if the repository is publicly accessible. Diffs can have an empty diff string if diff limits are reached.

GET /projects/:id/repository/compare

Supported attributes:

AttributeTypeRequiredDescription
fromstringYesCommit SHA or branch name.
idinteger or stringYesID or URL-encoded path of the project.
tostringYesCommit SHA or branch name.
from_project_idintegerNoID to compare from.
straightbooleanNoIf true, comparison method is direct comparison between from and to (from..to). If false, compare using merge base (fromto). Default is false.
unidiffbooleanNoIf true, present diffs in the unified diff format. Default is false. Introduced in GitLab 16.5.

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

AttributeTypeDescription
commitobjectDetails of the latest commit in the comparison.
commitsobject arrayList of commits included in the comparison.
commits[].author_emailstringCommit author’s email address.
commits[].author_namestringCommit author’s name.
commits[].created_atdatetimeCommit creation timestamp.
commits[].idstringFull commit SHA.
commits[].short_idstringShort commit SHA.
commits[].titlestringCommit title.
compare_same_refbooleanIf true, comparison uses the same reference for both from and to.
compare_timeoutbooleanIf true, comparison operation timed out.
diffsobject arrayList of file differences.
diffs[].a_modestringOld file mode.
diffs[].b_modestringNew file mode.
diffs[].collapsedbooleanIf true, file diffs are excluded but can be fetched on request.
diffs[].deleted_filebooleanIf true, file has been removed.
diffs[].diffstringDiff content showing changes made to the file.
diffs[].new_filebooleanIf true, file has been added.
diffs[].new_pathstringNew path of the file.
diffs[].old_pathstringOld path of the file.
diffs[].renamed_filebooleanIf true, file has been renamed.
diffs[].too_largebooleanIf true, file diffs are excluded and cannot be retrieved.
web_urlstringWeb URL for viewing the comparison.

Example request:

curl --header "PRIVATE-TOKEN: <your_access_token>" \
  --url "https://gitlab.example.com/api/v4/projects/5/repository/compare?from=main&to=feature"

Example response:

{
  "commit": {
    "id": "12d65c8dd2b2676fa3ac47d955accc085a37a9c1",
    "short_id": "12d65c8dd2b",
    "title": "JS fix",
    "author_name": "Example User",
    "author_email": "user@example.com",
    "created_at": "2014-02-27T10:27:00+02:00"
  },
  "commits": [{
    "id": "12d65c8dd2b2676fa3ac47d955accc085a37a9c1",
    "short_id": "12d65c8dd2b",
    "title": "JS fix",
    "author_name": "Example User",
    "author_email": "user@example.com",
    "created_at": "2014-02-27T10:27:00+02:00"
  }],
  "diffs": [{
    "old_path": "files/js/application.js",
    "new_path": "files/js/application.js",
    "a_mode": null,
    "b_mode": "100644",
    "diff": "@@ -24,8 +24,10 @@\n //= require g.raphael-min\n //= require g.bar-min\n //= require branch-graph\n-//= require highlightjs.min\n-//= require ace/ace\n //= require_tree .\n //= require d3\n //= require underscore\n+\n+function fix() { \n+  alert(\"Fixed\")\n+}",
    "collapsed": false,
    "too_large": false,
    "new_file": false,
    "renamed_file": false,
    "deleted_file": false
  }],
  "compare_timeout": false,
  "compare_same_ref": false,
  "web_url": "https://gitlab.example.com/janedoe/gitlab-foss/-/compare/ae73cb07c9eeaf35924a10f713b364d32b2dd34f...0b4bc9a49b562e85de7cc9e834518ea6828729b9"
}

Get contributor list

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

The commit count returned does not include merge commits.

GET /projects/:id/repository/contributors

Supported attributes:

AttributeTypeRequiredDescription
idinteger or stringYesID or URL-encoded path of the project.
order_bystringNoOrder contributors by name, email, or commits (number of commits). If not specified, contributors are ordered by commit date.
refstringNoName of a repository branch or tag. If not given, the default branch.
sortstringNoReturn contributors sorted in asc or desc order. Default is asc.

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

AttributeTypeDescription
additionsintegerNumber of line additions by the contributor.
commitsintegerNumber of commits by the contributor.
deletionsintegerNumber of line deletions by the contributor.
emailstringEmail address of the contributor.
namestringName of the contributor.

Example request:

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

Example response:

[{
  "name": "Example User",
  "email": "example@example.com",
  "commits": 117,
  "additions": 0,
  "deletions": 0
}, {
  "name": "Sample User",
  "email": "sample@example.com",
  "commits": 33,
  "additions": 0,
  "deletions": 0
}]

Get merge base

Get the common ancestor for 2 or more refs, such as commit SHAs, branch names, or tags.

GET /projects/:id/repository/merge_base

Supported attributes:

AttributeTypeRequiredDescription
idinteger or stringYesID or URL-encoded path of the project.
refsarrayYesRefs to find the common ancestor of. Accepts multiple refs.

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

AttributeTypeDescription
author_emailstringAuthor’s email address.
author_namestringAuthor’s name.
authored_datedatetimeDate when the commit was authored.
committed_datedatetimeDate when the commit was committed.
committer_emailstringCommitter’s email address.
committer_namestringCommitter’s name.
created_atdatetimeCommit creation timestamp.
extended_trailersobjectExtended information about Git trailers.
idstringFull commit SHA.
messagestringFull commit message.
parent_idsarrayList of parent commit SHAs.
short_idstringShort commit SHA.
titlestringCommit title.
trailersobjectGit trailers parsed from the commit message.
web_urlstringURL to view the commit in the GitLab web interface.

Example request, with the refs truncated for readability:

curl --header "PRIVATE-TOKEN: <your_access_token>" \
  --url "https://gitlab.example.com/api/v4/projects/5/repository/merge_base?refs[]=304d257d&refs[]=0031876f"

Example response:

{
  "id": "1a0b36b3cdad1d2ee32457c102a8c0b7056fa863",
  "short_id": "1a0b36b3",
  "title": "Initial commit",
  "created_at": "2014-02-27T08:03:18.000Z",
  "parent_ids": [],
  "message": "Initial commit\n",
  "author_name": "Example User",
  "author_email": "user@example.com",
  "authored_date": "2014-02-27T08:03:18.000Z",
  "committer_name": "Example User",
  "committer_email": "user@example.com",
  "committed_date": "2014-02-27T08:03:18.000Z",
  "trailers": {},
  "extended_trailers": {},
  "web_url": "https://gitlab.example.com/example-group/example-project/-/commit/1a0b36b3cdad1d2ee32457c102a8c0b7056fa863"
}

Generate changelog data

Generate changelog data based on commits in a repository, without committing them to a changelog file.

Works exactly like POST /projects/:id/repository/changelog, except the changelog data isn’t committed to any changelog file.

GET /projects/:id/repository/changelog

Supported attributes:

AttributeTypeRequiredDescription
versionstringYesVersion to generate the changelog for. Format must follow semantic versioning.
config_filestringNoPath of changelog configuration file in the project’s Git repository. Defaults to .gitlab/changelog_config.yml.
config_file_refstringNoGit reference (for example, branch) where the changelog configuration file is defined. Defaults to the default repository branch.
datedatetimeNoDate and time of the release. Uses ISO 8601 format. Example: 2016-03-11T03:45:40Z. Defaults to the current time.
fromstringNoStart of the range of commits (as a SHA) to use for generating the changelog. This commit itself isn’t included in the list.
tostringNoEnd of the range of commits (as a SHA) to use for the changelog. This commit is included in the list. Defaults to the HEAD of the default project branch.
trailerstringNoGit trailer to use for including commits. Defaults to Changelog.

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

AttributeTypeDescription
notesstringGenerated changelog data in Markdown format.

Example request:

curl --header "PRIVATE-TOKEN: token" \
  --url "https://gitlab.com/api/v4/projects/42/repository/changelog?version=1.0.0"

Example response, with line breaks added for readability:

{
  "notes": "## 1.0.0 (2021-11-17)\n\n### feature (2 changes)\n\n-
    [Title 2](namespace13/project13@ad608eb642124f5b3944ac0ac772fecaf570a6bf)
    ([merge request](namespace13/project13!2))\n-
    [Title 1](namespace13/project13@3c6b80ff7034fa0d585314e1571cc780596ce3c8)
    ([merge request](namespace13/project13!1))\n"
}

Add changelog data to file

Generate changelog data based on commits in a repository and commit them to a changelog file.

Given a semantic version and a range of commits, GitLab generates a changelog for all commits that use a particular Git trailer. GitLab adds a new Markdown-formatted section to a changelog file in the Git repository of the project. The output format can be customized.

For performance and security reasons, parsing the changelog configuration is limited to 2 seconds. This limitation helps prevent potential DoS attacks from malformed changelog templates. If the request times out, consider reducing the size of your changelog_config.yml file.

For user-facing documentation, see Changelogs.

POST /projects/:id/repository/changelog

Changelogs support the following attributes:

AttributeTypeRequiredDescription
version 1stringYesVersion to generate the changelog for. Format must follow semantic versioning.
branchstringNoBranch to commit the changelog changes to. Defaults to the project’s default branch.
config_filestringNoPath to the changelog configuration file in the project’s Git repository. Defaults to .gitlab/changelog_config.yml.
config_file_refstringNoGit reference (for example, branch) where the changelog configuration file is defined. Defaults to the default repository branch.
datedatetimeNoDate and time of the release. Defaults to the current time.
filestringNoFile to commit the changes to. Defaults to CHANGELOG.md.
from 2stringNoSHA of the commit that marks the beginning of the range of commits to include in the changelog. This commit isn’t included in the changelog.
messagestringNoCommit message to use when committing the changes. Defaults to Add changelog for version X, where X is the value of the version argument.
tostringNoSHA of the commit that marks the end of the range of commits to include in the changelog. This commit is included in the changelog. Defaults to the branch specified in the branch attribute. Limited to 15000 commits.
trailerstringNoGit trailer to use for including commits. Defaults to Changelog. Case-sensitive: Example does not match example or eXaMpLE.

Footnotes:

  1. The version attribute can include or omit the v prefix. Both 1.0.0 and v1.0.0 produce identical results. Introduced in GitLab 17.0.

  2. When from is unspecified, GitLab automatically finds the last stable version tag that precedes your specified version. GitLab recognizes tags in X.Y.Z or vX.Y.Z format, following semantic versioning.

    For example, if version is 2.1.0, GitLab uses tag v2.0.0. When version is 1.1.1 or 1.2.0, GitLab uses tag v1.1.0. Pre-release tags like v1.0.0-pre1 are ignored.

    If no suitable tag is found, the API returns an error and you must explicitly specify the from attribute.

Examples

These examples use cURL to perform HTTP requests. The example commands use these values:

  • Project ID: 42
  • Location: hosted on GitLab.com
  • Example API token: token

This command generates a changelog for version 1.0.0.

The commit range:

  • Starts with the tag of the last release.
  • Ends with the last commit on the target branch. The default target branch is the project’s default branch.

If the last tag is v0.9.0 and the default branch is main, the range of commits included in this example is v0.9.0..main:

curl --request POST \
  --header "PRIVATE-TOKEN: token" \
  --data "version=1.0.0" \
  --url "https://gitlab.com/api/v4/projects/42/repository/changelog"

To generate the data on a different branch, specify the branch parameter. This command generates data from the foo branch:

curl --request POST \
  --header "PRIVATE-TOKEN: token" \
  --data "version=1.0.0&branch=foo" \
  --url "https://gitlab.com/api/v4/projects/42/repository/changelog"

To use a different trailer, use the trailer parameter:

curl --request POST --header "PRIVATE-TOKEN: token" \
  --data "version=1.0.0&trailer=Type" \
  --url "https://gitlab.com/api/v4/projects/42/repository/changelog"

To store the results in a different file, use the file parameter:

curl --request POST \
  --header "PRIVATE-TOKEN: token" \
  --data "version=1.0.0&file=NEWS" \
  --url "https://gitlab.com/api/v4/projects/42/repository/changelog"

To specify a branch as a parameter, use the to attribute:

curl --request GET \
  --header "PRIVATE-TOKEN: token" \
  --url "https://gitlab.com/api/v4/projects/42/repository/changelog?version=1.0.0&to=release/x.x.x"

Migrate from manual changelog files

When you migrate from an existing manually-managed changelog file to one that uses Git trailers, make sure that the changelog file matches the expected format. Otherwise, new changelog entries added by the API might be inserted in an unexpected position. For example, if the version values in the manually-managed changelog file are specified as vX.Y.Z instead of X.Y.Z, then new changelog entries added using Git trailers are appended to the end of the changelog file.

Issue 444183 proposes customizing the version header format in changelog files. However, until that issue has been completed, the expected version header format in changelog files is X.Y.Z.

Health

Get statistics related to the health of a project repository. This endpoint is rate-limited to 5 requests/hour per project.

GET /projects/:id/repository/health

Supported attributes:

AttributeTypeRequiredDescription
generatebooleanNoIf true, a new health report should be generated. Set this if the endpoint returns 404.

If successful, returns 200 OK and repository health statistics.

Example request:

curl --header "PRIVATE-TOKEN: token" \
  --url "https://gitlab.com/api/v4/projects/42/repository/health"

Example response:

{
  "size": 2619748827,
  "references": {
    "loose_count": 13,
    "packed_size": 333978,
    "reference_backend": "REFERENCE_BACKEND_FILES"
  },
  "objects": {
    "size": 2180475409,
    "recent_size": 2180453999,
    "stale_size": 21410,
    "keep_size": 0,
    "packfile_count": 1,
    "reverse_index_count": 1,
    "cruft_count": 0,
    "keep_count": 0,
    "loose_objects_count": 36,
    "stale_loose_objects_count": 36,
    "loose_objects_garbage_count": 0
  },
  "commit_graph": {
    "commit_graph_chain_length": 1,
    "has_bloom_filters": true,
    "has_generation_data": true,
    "has_generation_data_overflow": false
  },
  "bitmap": null,
  "multi_pack_index": {
    "packfile_count": 1,
    "version": 1
  },
  "multi_pack_index_bitmap": {
    "has_hash_cache": true,
    "has_lookup_table": true,
    "version": 1
  },
  "alternates": null,
  "is_object_pool": false,
  "last_full_repack": {
    "seconds": 1745892013,
    "nanos": 0
  },
  "updated_at": "2025-05-14T02:31:08.022Z"
}

For a description of each field in the response, see the RepositoryInfoResponse protobuf message.