296.md 7.0 KB
Newer Older
Lab机器人's avatar
readme  
Lab机器人 已提交
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156
# 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" 
```