企业🤖AI智能体构建引擎,智能编排和调试,一键部署,支持私有化部署方案 广告
# Branches API > 原文:[https://docs.gitlab.com/ee/api/branches.html](https://docs.gitlab.com/ee/api/branches.html) * [List repository branches](#list-repository-branches) * [Get single repository branch](#get-single-repository-branch) * [Protect repository branch](#protect-repository-branch) * [Unprotect repository branch](#unprotect-repository-branch) * [Create repository branch](#create-repository-branch) * [Delete repository branch](#delete-repository-branch) * [Delete merged branches](#delete-merged-branches) # Branches API[](#branches-api "Permalink") 该 API 在[存储库分支上运行](../user/project/repository/branches/index.html) . **提示:**另请参阅[受保护的分支机构 API](protected_branches.html) . ## List repository branches[](#list-repository-branches "Permalink") 从项目中获取存储库分支的列表,按名称的字母顺序排序. **注意:**如果可公开访问该存储库,则无需身份验证即可访问此端点. ``` GET /projects/:id/repository/branches ``` Parameters: | Attribute | Type | Required | Description | | --- | --- | --- | --- | | `id` | integer/string | yes | 身份验证用户拥有[的项目的](README.html#namespaced-path-encoding) ID 或[URL 编码路径](README.html#namespaced-path-encoding) . | | `search` | string | no | 返回包含搜索字符串的分支列表. 您可以使用`^term`和`term$`查找分别以`term`开头和结尾的分支. | 请求示例: ``` curl --header "PRIVATE-TOKEN: <your_access_token>" "https://gitlab.example.com/api/v4/projects/5/repository/branches" ``` 响应示例: ``` [ { "name": "master", "merged": false, "protected": true, "default": true, "developers_can_push": false, "developers_can_merge": false, "can_push": true, "web_url": "http://gitlab.example.com/my-group/my-project/-/tree/master", "commit": { "author_email": "john@example.com", "author_name": "John Smith", "authored_date": "2012-06-27T05:51:39-07:00", "committed_date": "2012-06-28T03:44:20-07:00", "committer_email": "john@example.com", "committer_name": "John Smith", "id": "7b5c3cc8be40ee161ae89a06bba6229da1032a0c", "short_id": "7b5c3cc", "title": "add projects API", "message": "add projects API", "parent_ids": [ "4ad91d3c1144c406e50c7b33bae684bd6837faf8" ] } }, ... ] ``` ## Get single repository branch[](#get-single-repository-branch "Permalink") 获取单个项目存储库分支. **注意:**如果可公开访问该存储库,则无需身份验证即可访问此端点. ``` GET /projects/:id/repository/branches/:branch ``` Parameters: | Attribute | Type | Required | Description | | --- | --- | --- | --- | | `id` | integer/string | yes | 身份验证用户拥有[的项目的](README.html#namespaced-path-encoding) ID 或[URL 编码路径](README.html#namespaced-path-encoding) . | | `branch` | string | yes | 分支名称. | 请求示例: ``` curl --header "PRIVATE-TOKEN: <your_access_token>" "https://gitlab.example.com/api/v4/projects/5/repository/branches/master" ``` 响应示例: ``` { "name": "master", "merged": false, "protected": true, "default": true, "developers_can_push": false, "developers_can_merge": false, "can_push": true, "web_url": "http://gitlab.example.com/my-group/my-project/-/tree/master", "commit": { "author_email": "john@example.com", "author_name": "John Smith", "authored_date": "2012-06-27T05:51:39-07:00", "committed_date": "2012-06-28T03:44:20-07:00", "committer_email": "john@example.com", "committer_name": "John Smith", "id": "7b5c3cc8be40ee161ae89a06bba6229da1032a0c", "short_id": "7b5c3cc", "title": "add projects API", "message": "add projects API", "parent_ids": [ "4ad91d3c1144c406e50c7b33bae684bd6837faf8" ] } } ``` ## Protect repository branch[](#protect-repository-branch "Permalink") 有关保护存储库分支的信息,请参见[`POST /projects/:id/protected_branches`](protected_branches.html#protect-repository-branches) . ## Unprotect repository branch[](#unprotect-repository-branch "Permalink") 有关取消保护存储库分支的信息,请参见[`DELETE /projects/:id/protected_branches/:name`](protected_branches.html#unprotect-repository-branches) . ## Create repository branch[](#create-repository-branch "Permalink") 在存储库中创建一个新分支. ``` POST /projects/:id/repository/branches ``` Parameters: | Attribute | Type | Required | Description | | --- | --- | --- | --- | | `id` | integer | yes | 身份验证用户拥有[的项目的](README.html#namespaced-path-encoding) ID 或[URL 编码路径](README.html#namespaced-path-encoding) . | | `branch` | string | yes | 分支名称. | | `ref` | string | yes | 分支名称或提交 SHA 以从中创建分支. | 请求示例: ``` curl --request POST --header "PRIVATE-TOKEN: <your_access_token>" "https://gitlab.example.com/api/v4/projects/5/repository/branches?branch=newbranch&ref=master" ``` 响应示例: ``` { "commit": { "author_email": "john@example.com", "author_name": "John Smith", "authored_date": "2012-06-27T05:51:39-07:00", "committed_date": "2012-06-28T03:44:20-07:00", "committer_email": "john@example.com", "committer_name": "John Smith", "id": "7b5c3cc8be40ee161ae89a06bba6229da1032a0c", "short_id": "7b5c3cc", "title": "add projects API", "message": "add projects API", "parent_ids": [ "4ad91d3c1144c406e50c7b33bae684bd6837faf8" ] }, "name": "newbranch", "merged": false, "protected": false, "default": false, "developers_can_push": false, "developers_can_merge": false, "can_push": true, "web_url": "http://gitlab.example.com/my-group/my-project/-/tree/newbranch" } ``` ## Delete repository branch[](#delete-repository-branch "Permalink") 从存储库中删除一个分支. **注意:**如果发生错误,将提供说明消息. ``` DELETE /projects/:id/repository/branches/:branch ``` Parameters: | Attribute | Type | Required | Description | | --- | --- | --- | --- | | `id` | integer/string | yes | 身份验证用户拥有[的项目的](README.html#namespaced-path-encoding) ID 或[URL 编码路径](README.html#namespaced-path-encoding) . | | `branch` | string | yes | 分支名称. | 请求示例: ``` curl --request DELETE --header "PRIVATE-TOKEN: <your_access_token>" "https://gitlab.example.com/api/v4/projects/5/repository/branches/newbranch" ``` ## Delete merged branches[](#delete-merged-branches "Permalink") Will delete all branches that are merged into the project’s default branch. **注意:**此操作不会删除[受保护的分支](../user/project/protected_branches.html) . ``` DELETE /projects/:id/repository/merged_branches ``` Parameters: | Attribute | Type | Required | Description | | --- | --- | --- | --- | | `id` | integer/string | yes | 身份验证用户拥有[的项目的](README.html#namespaced-path-encoding) ID 或[URL 编码路径](README.html#namespaced-path-encoding) . | 请求示例: ``` curl --request DELETE --header "PRIVATE-TOKEN: <your_access_token>" "https://gitlab.example.com/api/v4/projects/5/repository/merged_branches" ```