The V3 API mentions branches in its reference page
The ref in the URL must be formatted as
heads/branch, not justbranch.
For example, the call to get the data for a branch namedsc/featureAwould 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/headsCopy the revision hash
Do a POST request from Hurl to
https://api.github.com/repos/<AUTHOR>/<REPO>/git/refswith the following as thePOSTbody :{ "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!
In 2022, you can also use 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'
Answer from VonC on Stack OverflowThe V3 API mentions branches in its reference page
The ref in the URL must be formatted as
heads/branch, not justbranch.
For example, the call to get the data for a branch namedsc/featureAwould 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/headsCopy the revision hash
Do a POST request from Hurl to
https://api.github.com/repos/<AUTHOR>/<REPO>/git/refswith the following as thePOSTbody :{ "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!
In 2022, you can also use 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'
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)
Is there a method to create a branch?
c# - Creating a new branch using Github API - Stack Overflow
Create a new branch via API, objectID
curl - Creating a new branch with GitHub API and php - Stack Overflow
Videos
Hi
I am doing some scripting for some automation. I need to use the DevOps API. And after being able to download a repo as a .zip-file, I thought I had the hang of this API stuff. But current status proves otherwise.
My goal is to create a new branch of an existing repository, and dump some files there.
Looking in the documentation, expecting to find what I need in git/repositories/branch/create gave nothing and googling further gave me this answer: https://github.com/Microsoft/azure-devops-node-api/issues/259 (and more specifically https://github.com/Microsoft/azure-devops-node-api/issues/259#issuecomment-466039209 ) that refs are branches, and I attribute that lack of knowledge to my superficial use of git.
But fair, he links to this site, and the example is here: https://docs.microsoft.com/en-us/rest/api/azure/devops/git/refs/update%20refs?view=azure-devops-rest-4.1#create/update/delete-a-ref-by-repositoryid
where the part I need is:
{
"name": "refs/heads/live",
"oldObjectId": "0000000000000000000000000000000000000000",
"newObjectId": "4b223e9c93ec3b6aaa6499f06e3ebb7c702e6106"
},As he mentions oldObjectID should be all zeros; makes sense. But where does the newObjectId come from? Should I just make a new 40 character long randomish hex string for each time I run my script?