分支 API

  • Tier: 基础版,专业版,旗舰版
  • Offering: JihuLab.com,私有化部署

该 API 在 代码库分支 上操作。

另请参阅 受保护分支 API

列出代码库分支#

从项目中获取代码库分支列表,按名称字母顺序排序。

如果代码库是公开访问的,则可以在不进行身份验证的情况下访问此端点。
plaintext
GET /projects/:id/repository/branches

参数:

属性类型必需描述
idinteger 或 string项目的 ID 或 URL 编码路径
searchstring返回包含搜索字符串的分支列表。使用 ^term 查找以 term 开头的分支,使用 term$ 查找以 term 结尾的分支。
regexstring返回名称匹配 re2 正则表达式的分支列表。

示例请求:

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

示例响应:

json
1[ 2 { 3 "name": "main", 4 "merged": false, 5 "protected": true, 6 "default": true, 7 "developers_can_push": false, 8 "developers_can_merge": false, 9 "can_push": true, 10 "web_url": "https://gitlab.example.com/my-group/my-project/-/tree/main", 11 "commit": { 12 "id": "7b5c3cc8be40ee161ae89a06bba6229da1032a0c", 13 "short_id": "7b5c3cc", 14 "created_at": "2024-06-28T03:44:20-07:00", 15 "parent_ids": [ 16 "4ad91d3c1144c406e50c7b33bae684bd6837faf8" 17 ], 18 "title": "add projects API", 19 "message": "add projects API", 20 "author_name": "John Smith", 21 "author_email": "john@example.com", 22 "authored_date": "2024-06-27T05:51:39-07:00", 23 "committer_name": "John Smith", 24 "committer_email": "john@example.com", 25 "committed_date": "2024-06-28T03:44:20-07:00", 26 "trailers": {}, 27 "extended_trailers": {}, 28 "web_url": "https://gitlab.example.com/my-group/my-project/-/commit/7b5c3cc8be40ee161ae89a06bba6229da1032a0c" 29 } 30 }, 31 ... 32]

获取单个代码库分支#

获取单个项目代码库分支。

如果代码库是公开访问的,则可以在不进行身份验证的情况下访问此端点。
plaintext
GET /projects/:id/repository/branches/:branch

参数:

属性类型必需描述
idinteger 或 string项目的 ID 或 URL 编码路径
branchstring分支的 URL 编码名称

示例请求:

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

示例响应:

json
1{ 2 "name": "main", 3 "merged": false, 4 "protected": true, 5 "default": true, 6 "developers_can_push": false, 7 "developers_can_merge": false, 8 "can_push": true, 9 "web_url": "https://gitlab.example.com/my-group/my-project/-/tree/main", 10 "commit": { 11 "id": "7b5c3cc8be40ee161ae89a06bba6229da1032a0c", 12 "short_id": "7b5c3cc", 13 "created_at": "2012-06-28T03:44:20-07:00", 14 "parent_ids": [ 15 "4ad91d3c1144c406e50c7b33bae684bd6837faf8" 16 ], 17 "title": "add projects API", 18 "message": "add projects API", 19 "author_name": "John Smith", 20 "author_email": "john@example.com", 21 "authored_date": "2012-06-27T05:51:39-07:00", 22 "committer_name": "John Smith", 23 "committer_email": "john@example.com", 24 "committed_date": "2012-06-28T03:44:20-07:00", 25 "trailers": {}, 26 "web_url": "https://gitlab.example.com/my-group/my-project/-/commit/7b5c3cc8be40ee161ae89a06bba6229da1032a0c" 27 } 28}

保护代码库分支#

有关保护代码库分支的信息,请参阅 POST /projects/:id/protected_branches

取消保护代码库分支#

有关取消保护代码库分支的信息,请参阅 DELETE /projects/:id/protected_branches/:name

创建代码库分支#

在代码库中创建一个新分支。

plaintext
POST /projects/:id/repository/branches

参数:

属性类型必需描述
idinteger项目的 ID 或 URL 编码路径
branchstring分支名称。
refstring要从中创建分支的分支名称或提交 SHA。

示例请求:

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

示例响应:

json
1{ 2 "commit": { 3 "id": "7b5c3cc8be40ee161ae89a06bba6229da1032a0c", 4 "short_id": "7b5c3cc", 5 "created_at": "2012-06-28T03:44:20-07:00", 6 "parent_ids": [ 7 "4ad91d3c1144c406e50c7b33bae684bd6837faf8" 8 ], 9 "title": "add projects API", 10 "message": "add projects API", 11 "author_name": "John Smith", 12 "author_email": "john@example.com", 13 "authored_date": "2012-06-27T05:51:39-07:00", 14 "committer_name": "John Smith", 15 "committer_email": "john@example.com", 16 "committed_date": "2012-06-28T03:44:20-07:00", 17 "trailers": {}, 18 "web_url": "https://gitlab.example.com/my-group/my-project/-/commit/7b5c3cc8be40ee161ae89a06bba6229da1032a0c" 19 }, 20 "name": "newbranch", 21 "merged": false, 22 "protected": false, 23 "default": false, 24 "developers_can_push": false, 25 "developers_can_merge": false, 26 "can_push": true, 27 "web_url": "https://gitlab.example.com/my-group/my-project/-/tree/newbranch" 28}

删除代码库分支#

从代码库中删除分支。

如果出现错误,会提供解释消息。
plaintext
DELETE /projects/:id/repository/branches/:branch

参数:

属性类型必需描述
idinteger/string项目的 ID 或 URL 编码路径
branchstring分支名称。

示例请求:

shell
curl --request DELETE \ --header "PRIVATE-TOKEN: <your_access_token>" \ --url "https://gitlab.example.com/api/v4/projects/5/repository/branches/newbranch"
删除分支并不会完全擦除所有相关数据。某些信息仍然存在以保持项目历史记录并支持恢复过程。有关更多信息,请参阅 [处理敏感信息](../topics/git/undo.md#handle-sensitive-information)。

删除已合并的分支#

删除所有合并到项目默认分支中的分支。

[受保护分支](../user/project/repository/branches/protected.md)不会作为此操作的一部分被删除。
plaintext
DELETE /projects/:id/repository/merged_branches

参数:

属性类型必需描述
idinteger/string项目的 ID 或 URL 编码路径

示例请求:

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