Github API - 创建分支?

问题描述 投票:0回答:5

似乎 v1、v2 和 v3 的“Repos”文档中缺少它......如何使用 Github API 创建分支?

git github
5个回答
105
投票

V3 API 在其参考页面中提到了分支

URL 中的引用格式必须为

heads/branch
,而不仅仅是
branch

例如,获取名为
sc/featureA
的分支的数据的调用将是:

GET /repos/:user/:repo/git/refs/heads/sc/featureA

创建参考

POST /repos/:user/:repo/git/refs

参数

ref

完全限定引用的名称字符串(即:refs/heads/master)。如果它不以“refs”开头并且至少有两个斜杠,它将被拒绝。

sha

将此引用设置为的 SHA1 值的字符串

因此应该可以通过在 ref 参数中命名新的“

/heads
”来创建一个新分支。


Potherca 指出工作测试,使用

www.hurl.it
的服务(发出HTTP请求)

  • 找到您想要分支的版本。
    无论是在 Github 上还是通过 Hurl 执行 GET 请求:

    https://api.github.com/repos/<AUTHOR>/<REPO>/git/refs/heads

  • 复制修订哈希值

  • 从 Hurl 向

    https://api.github.com/repos/<AUTHOR>/<REPO>/git/refs
    发送 POST 请求,并将以下内容作为
    POST
    正文:

      {
          "ref": "refs/heads/<NEW-BRANCH-NAME>",
          "sha": "<HASH-TO-BRANCH-FROM>"
      }
    

    (显然将

    <NEW-BRANCH-NAME>
    替换为您希望新分支具有的名称,并将
    <HASH-TO-BRANCH-FROM>
    替换为您想要分支的修订版本的哈希值)

    您需要使用 HTTP basic 并填写您的 Github 凭据才能访问 Github API。

  • 按下发送按钮,您的分支将被创建!


2022年,你还可以使用gh api

gh api \
  --method POST \
  -H "Accept: application/vnd.github.v3+json" \
  /repos/OWNER/REPO/git/refs \
  -f ref='refs/heads/featureA'
 -f sha='aa218f56b14c9653891f9e74264a383fa43fefbd'

7
投票

添加@VonC答案,这是Python中的工作片段。

import requests
headers = {'Authorization': "Token " + 'YOUR_TOKEN_HERE'}
url = "https://api.github.com/repos/<USERNAME>/<REPO>/git/refs/heads"
branches = requests.get(url, headers=headers).json()
branch, sha = branches[-1]['ref'], branches[-1]['object']['sha']
res = requests.post('https://api.github.com/repos/<USERNAME>/<REPO>/git/refs', json={
    "ref": "refs/heads/newbranch",
    "sha": sha
}, headers=headers)
print(res.content)

5
投票

这是我们在GitHub上创建分支的API时所有同学都会遇到的问题

{
  "message": "Not Found",
  "documentation_url": "https://developer.github.com/v3"
}

为了解决在 Github 中创建存储库时出现的错误.....

  1. 首先在

    中创建个人令牌

    Github=>设置=>developerOption=>生成个人令牌...

                     or 
    

    在 gitLogin bu Oauth 期间,当您传递 client_id 时,您就通过了 scope=repo(因为当您使用 token 或 任何东西)

  2. 之后:点击API(获取)

    https://api.github.com/repos/<your login name>/<Your Repository Name>/git/refs/heads

  3. 您收到的回复类似于

    Response => {
    [
    {
        "ref": "refs/heads/<already present branch name for ref>",
        "node_id": "jkdhoOIHOO65464edg66464GNLNLnlnnlnlna==",
        "url": " https://api.github.com/repos/<your login name>/<Your Repository Name>/git/refs/heads/<already present branch name for ref>",
        "object": {
            "sha": "guDSGss85s1KBih546465kkbNNKKbkSGyjes56",
            "type": "commit",
            "url": " https://api.github.com/repos/<your login name>/<Your Repository Name>/git/commits/guDSGss85s1KBih546465kkbNNKKbkSGyjes56"
        }
    }
    ]
    }
    
  4. 再次点击API(发布)完成此过程

    https://api.github.com/repos/Bhupi2508/Test/git/refs...
    

    并以 JSON 格式发送数据,如下所示:

    {
        "ref": "refs/heads/<new branch name>",
        "sha": "4661616ikgohlKIKHBK4634GRGSD66"
    }
    

    然后你通过 API 在 GITHUB 中创建一个分支

    删除分支的过程仅命中 DELETE(第一个)API


4
投票

努力为私人回购做到这一点,从而回答相同案例的问题:

对于私人存储库,您需要通过“用户名”和“密码”或 OAUTH 对请求进行身份验证。以下步骤;

  1. 在 github 开发者设置中找到您帐户的个人访问令牌。

  2. 使用基本分支名称发出经过身份验证的 GET 请求。

  3. 从 GET 请求的响应中使用

    jq
    过滤提交 SHA。

  4. 在 github 的 POST 请求中发布新的分支名称并提交 SHA 作为正文。

行动中:

TOKEN=<GITHUB-AUTH-TOKEN-VALUE>
Previous_branch_name=ABC
New_branch_name=XYZ

SHA=$(curl -H "Authorization: token $TOKEN" https://api.github.com/repos/<REPO>/git/refs/heads/$Previous_branch_name | jq -r '.object.sha')

curl -X POST -H "Authorization: token $TOKEN" \
-d  "{\"ref\": \"refs/heads/$New_branch_name\",\"sha\": \"$SHA\"}"  https://api.github.com/repos/<REPO>/git/refs

0
投票

这是列出给定存储库中所有当前

refs
的另一种方法。该命令使用了 GitHub API (brew install gh)

 gh api repos/<ONWER>/<REPO>/git/refs
© www.soinside.com 2019 - 2024. All rights reserved.