Use this API to manage repository files. You can also configure rate limits for this API.

Retrieve a file from a repository

GET /api/v4/projects/{id}/repository/files/{file_path}

Retrieves information about a specified file in a repository. This includes information like the name, size, and the file contents. File content is Base64 encoded. You can access this endpoint without authentication if the repository is publicly accessible. For blobs larger than 10 MB, this endpoint has a rate limit of 5 requests every minute for each project. For text files not stored as valid UTF-8 in Git, content is converted to UTF-8 before Base64 encoding. In that case, size is the raw blob size, which might differ from the length of the decoded content, and content_sha256 is the hash of the converted content, not the raw blob. To retrieve the exact bytes stored in Git, use the raw file endpoint.

Parameters

NameTypeDescription
id
Path, required
StringID or URL-encoded path of the project
Example: gitlab-org/gitlab
file_path
Path, required
StringURL-encoded full path to the file, for example lib%2Fclass%2Erb
Example: lib%2Fclass%2Erb
ref
Query, required
StringCommit SHA or name of a repository branch or tag. Use HEAD to use the default branch
Example: main

Responses

CodeDescriptionSchema
200OK—
400Bad Request—
404Not Found—

Create a file in a repository

POST /api/v4/projects/{id}/repository/files/{file_path}

Creates a file in a specified repository. Use the Commits API to create multiple files with a single request. Requests larger than 300 MB by default are rejected. Requests larger than 20 MB are rate-limited to 3 requests every 30 seconds for each user.

Parameters

NameTypeDescription
id
Path, required
StringID or URL-encoded path of the project
Example: gitlab-org/gitlab
file_path
Path, required
StringURL-encoded full path to the file, for example lib%2Fclass%2Erb
Example: lib%2Fclass%2Erb

Request body (multipart/form-data)

PropertyTypeDescription
file
Required
String (binary)The file content to be created (generated by Multipart middleware)

Responses

CodeDescriptionSchema
201Created—
400Bad Request—
404Not Found—

Update a file in a repository

PUT /api/v4/projects/{id}/repository/files/{file_path}

Updates a specified file in a repository. Use the Commits API to update multiple files with a single request. Requests larger than 300 MB by default are rejected. Requests larger than 20 MB are rate-limited to 3 requests every 30 seconds for each user. If the commit fails, returns 400 Bad Request with an error message. Possible causes are a file_path that contains /../, new file contents that are identical to the current contents, or a git push to the branch while the edit was in progress.

Parameters

NameTypeDescription
id
Path, required
StringID or URL-encoded path of the project
Example: gitlab-org/gitlab
file_path
Path, required
StringURL-encoded full path to the file, for example lib%2Fclass%2Erb
Example: lib%2Fclass%2Erb

Request body (multipart/form-data)

PropertyTypeDescription
file
Required
String (binary)The file content to be updated (generated by Multipart middleware)

Responses

CodeDescriptionSchema
200OK—
400Bad Request—
404Not Found—

Delete a file in a repository

DELETE /api/v4/projects/{id}/repository/files/{file_path}

Deletes a specified file in a repository. Use the Commits API to delete multiple files with a single request.

Parameters

NameTypeDescription
id
Path, required
StringID or URL-encoded path of the project
Example: gitlab-org/gitlab
file_path
Path, required
StringURL-encoded full path to the file, for example lib%2Fclass%2Erb
Example: lib%2Fclass%2Erb
branch
Query, required
StringName of the branch to commit the deletion into. Combine with start_branch to create a new branch
Example: main
commit_message
Query, required
StringCommit message
Example: Initial commit
start_branch
Query
StringName of the base branch to create the branch from
Example: main
author_email
Query
StringEmail address of the commit author
Example: johndoe@example.com
author_name
Query
StringName of the commit author
Example: John Doe
last_commit_id
Query
StringLast known file commit ID
Example: 2695effb5807a22ff3d138d593fd856244e155e7

Responses

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

Retrieve file metadata

HEAD /api/v4/projects/{id}/repository/files/{file_path}

Retrieves metadata for a specified file in a repository. Uses the HEAD method and returns the metadata in X-Gitlab-* response headers, such as X-Gitlab-Blob-Id, X-Gitlab-Commit-Id, X-Gitlab-Content-Sha256, X-Gitlab-File-Name, X-Gitlab-Last-Commit-Id, X-Gitlab-Ref, and X-Gitlab-Size.

Parameters

NameTypeDescription
id
Path, required
StringID or URL-encoded path of the project
Example: gitlab-org/gitlab
file_path
Path, required
StringURL-encoded full path to the file, for example lib%2Fclass%2Erb
Example: lib%2Fclass%2Erb

Request body (application/json)

PropertyTypeDescription
ref
Required
StringCommit SHA or name of a repository branch or tag
Example: main

Responses

CodeDescriptionSchema
200OK—
400Bad Request—
404Not Found—

Retrieve file blame history from a repository

GET /api/v4/projects/{id}/repository/files/{file_path}/blame

Retrieves blame history for a specified file in a repository. Each blame range contains lines and their corresponding commit information. This endpoint is rate-limited for each authenticated user for a given project, and for each IP address for unauthenticated requests. On GitLab Self-Managed and GitLab Dedicated, an administrator can change these limits in the application settings. The default limits are 400 requests every minute for each user and project, set by project_repositories_blame_limit, and 400 requests every minute for each IP address, set by project_repositories_blame_unauthenticated_limit. A limit of 0 disables it. HEAD requests do not count toward these limits. The GraphQL blame field on RepositoryBlob shares these limits.

Parameters

NameTypeDescription
id
Path, required
StringID or URL-encoded path of the project
Example: gitlab-org/gitlab
file_path
Path, required
StringURL-encoded full path to the file, for example lib%2Fclass%2Erb
Example: lib%2Fclass%2Erb
ref
Query, required
StringCommit SHA or name of a repository branch or tag. Use HEAD to use the default branch
Example: main
range
Query
ObjectRange of lines to return blame information for
range[start]
Query, required
IntegerFirst line number of the range to blame
Minimum: 1
range[end]
Query, required
IntegerLast line number of the range to blame
Minimum: 1

Responses

CodeDescriptionSchema
200OKAPIEntitiesBlameRange
400Bad Request—
404Not Found—
429Too many requests—

Retrieve file blame metadata

HEAD /api/v4/projects/{id}/repository/files/{file_path}/blame

Retrieves blame metadata for lines in a specified file. Uses the HEAD method and returns the metadata in X-Gitlab-* response headers, such as X-Gitlab-Blob-Id, X-Gitlab-Commit-Id, X-Gitlab-Content-Sha256, X-Gitlab-File-Name, X-Gitlab-Last-Commit-Id, X-Gitlab-Ref, and X-Gitlab-Size.

Parameters

NameTypeDescription
id
Path, required
StringID or URL-encoded path of the project
Example: gitlab-org/gitlab
file_path
Path, required
StringURL-encoded full path to the file, for example lib%2Fclass%2Erb
Example: lib%2Fclass%2Erb

Request body (application/json)

PropertyTypeDescription
ref
Required
StringCommit SHA or name of a repository branch or tag
Example: main

Responses

CodeDescriptionSchema
200OK—
400Bad Request—
404Not Found—

Retrieve a raw file from a repository

GET /api/v4/projects/{id}/repository/files/{file_path}/raw

Retrieves the raw file contents for a specified file in a repository. To get only the file metadata, use the HEAD method on the file endpoint.

Parameters

NameTypeDescription
id
Path, required
StringID or URL-encoded path of the project
Example: gitlab-org/gitlab
file_path
Path, required
StringURL-encoded full path to the file, for example lib%2Fclass%2Erb
Example: lib%2Fclass%2Erb
ref
Query
StringCommit SHA or name of a repository branch or tag. Default is the HEAD of the project
Example: main
lfs
Query
BooleanIf true, returns the Git LFS file contents instead of the pointer. Ignored if the file isn’t tracked by Git LFS
Default: false

Responses

CodeDescriptionSchema
200OKString (binary) (application/octet-stream)
400Bad Request—
404Not Found—

Schemas

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

APIEntitiesBlameRange

PropertyTypeDescription
commitAPIEntitiesBlameRangeCommitCommit that last changed the lines in this blame range
linesArray of stringsLines of the file in this blame range
Example: ["lorem ipsum"]

APIEntitiesBlameRangeCommit

PropertyTypeDescription
author_emailStringExample: john@example.com
author_nameStringExample: John Smith
authored_dateString (date-time)Example: 2012-05-28T04:42:42-07:00
committed_dateString (date-time)Example: 2012-05-28T04:42:42-07:00
committer_emailStringExample: jack@example.com
committer_nameStringExample: Jack Smith
idStringExample: 2695effb5807a22ff3d138d593fd856244e155e7
messageStringExample: Initial commit
parent_idsArray of stringsExample: ["2a4b78934375d7f53875269ffd4f45fd83a84ebe"]