Github API - create branch?

GitGithub

Git Problem Overview


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

Git Solutions


Solution 1 - Git

The V3 API mentions branches in its reference page

> 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/featureA would be:

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.

sha

> String of the SHA1 value to set this reference to

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


Potherca points out to a working test, using the service of www.hurl.it (which makes HTTP requests)

> - 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/refs with the following as the POST body :
> > { > "ref": "refs/heads/", > "sha": "" > } >
> (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!

Solution 2 - Git

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

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

For solving this error during create repository in 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)

  1. After that: hit the API(get)

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

  2. 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"    }}]
    }
    
  3. 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

Solution 3 - Git

Adding to @VonC answer, here is working snippet in 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)

Solution 4 - Git

Struggled to do this for Private repos thus answering the question for same cases:

For private repos you need to have the requests authenticated via "username" & "password" or OAUTH. Steps below;

  1. Find the Personal access token for your account in github developer settings.

  2. Make an authenticated GET request with base branch name.

  3. From the response of GET request filter the commit SHA using jq.

  4. Post the new branch name and commit SHA as body in POST request to github.

In Action:

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"

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
Questionuser375566View Question on Stackoverflow
Solution 1 - GitVonCView Answer on Stackoverflow
Solution 2 - GitBHUPENDRA SINGHView Answer on Stackoverflow
Solution 3 - GitNishant NawarkhedeView Answer on Stackoverflow
Solution 4 - GitGaurav DalalView Answer on Stackoverflow