git Github API - 创建分支?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/9506181/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-19 06:33:28  来源:igfitidea点击:

Github API - create branch?

gitgithub

提问by VonC

Seems like it's missing from the "Repos" docs for v1, v2, and v3...how do I create a branch using the Github API?

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

回答by VonC

The V3 API mentions branches in its reference page

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

The ref in the URL must be formatted as heads/branch, not just branch.
For example, the call to get the data for a branch named sc/featureAwould be:

URL 中的 ref 格式必须为heads/branch,而不仅仅是branch.
例如,获取名为分支的数据的调用sc/featureA将是:

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

Create a Reference

创建参考

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

Parameters

参数

ref

String of the name of the fully qualified reference (ie: refs/heads/master). If it doesn't start with ‘refs' and have at least two slashes, it will be rejected.

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

sha

String of the SHA1 value to set this reference to

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

So it should be possible to create a new branch, by naming a new '/heads' in the ref parameter.

所以应该可以通过/heads在 ref 参数中命名一个新的 ' '来创建一个新分支。



Pothercapoints outto a working test, using the service of www.hurl.it(which makes HTTP requests)

Potherca指出了一个工作测试,使用的服务www.hurl.it(它发出 HTTP 请求)

  • Find the revision you want to branch from.
    Either on Github itself or by doing a GET request from Hurl:

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

  • Copy the revision hash

  • Do a POST request from Hurl to https://api.github.com/repos/<AUTHOR>/<REPO>/git/refswith the following as the POSTbody :

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

    (obviously replacing the <NEW-BRANCH-NAME>with the name your want the new branch to have and the <HASH-TO-BRANCH-FROM>with, you know, the hash of the revision you want to branch from)

    You will need to use HTTP basic and fill in your Github credentials to access the Github API.

  • Press the Send button and your branch will be created!

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

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

  • 复制修订哈希

  • https://api.github.com/repos/<AUTHOR>/<REPO>/git/refs使用以下内容作为POST正文从 Hurl 执行 POST 请求:

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

    (显然,<NEW-BRANCH-NAME>用您希望新分支具有的名称替换 the以及您<HASH-TO-BRANCH-FROM>要从中分支的修订的哈希值)

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

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

回答by BHUPENDRA SINGH

This is a common problem for all the students when we create API for creating a branch in GitHub

这是我们在GitHub中创建用于创建分支的API时所有学生的共同问题

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

For solving this error during create repository in Github.....

为了在 Github 中创建存储库期间解决此错误.....

  1. First create a personal token in

    Github=>setting=>developerOption=>generatePersonalToken...

                     or 
    

    during gitLogin bu Oauth when you pass client_id that time you pass scope=repo(Because it's allow all the Repository when you used token or anything)

  2. After that: hit the API(get)

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

  3. You got a response which is like

    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. Complete this process again hit API (Post)

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

    And send data in JSON format like this:

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

    THEN YOU CREATE A BRANCH IN GITHUB By APIs

    And the process for Delete Branch hit only DELETE (first) APIs

  1. 首先在中创建一个个人令牌

    Github=>setting=>developerOption=>generatePersonalToken...

                     or 
    

    在 gitLogin bu Oauth 期间,当您传递 client_id 时传递 scope=repo(因为当您使用令牌或任何东西时,它允许所有存储库)

  2. 之后:点击 API(get)

    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(Post)

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

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

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

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

    并且 Delete Branch 的过程仅命中 DELETE (first) APIs