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/<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 Overflow
Top answer
1 of 5
109

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/<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'
2 of 5
8

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)
Discussions

Is there a method to create a branch?
I think might be useful to have a method to create a github branch. Or I was missed some docs to read? http://stackoverflow.com/questions/9506181/github-api-create-branch Thanks. More on github.com
🌐 github.com
1
January 24, 2016
c# - Creating a new branch using Github API - Stack Overflow
I am trying to create a new branch, update a file in that branch, and then create a PR all using the Git API. To create a branch, I understand that we need to use the Reference API for this, as men... More on stackoverflow.com
🌐 stackoverflow.com
July 7, 2022
Create a new branch via API, objectID
Generally we can create branch based on the existing branches or commits, so the newObjectId actually is the ObjectID of the specific branch or the commit ID. We can get the ObjectID of the existing branches with the following REST API: GET https://dev.azure.com/{organization}/{project}/_apis/git/repositories/{repository_ID}/refs?api-version=5.1 And get the commits with this REST API : GET https://dev.azure.com/{organization}/{project}/_apis/git/repositories/{repository_ID}/commits?api-version=5.1 Then we can create a new branch with the specific ObjectID or CommitID: Below PowerShell script for your reference to create new branch: Param( [string]$collectionurl = "https://dev.azure.com/{organization}", [string]$project = "projectname", [string]$repoid = "62c8ce54-a7bb-4e08-8ed7-40b27831bd8b", [string]$Newbranch_name = "AG1102", [string]$newObjectId= "Existing Branch objectID or the commit ID ", [string]$user = "username", [string]$token = "password/PAT" ) # Base64-encodes the Personal Access Token (PAT) appropriately $base64AuthInfo = [Convert]::ToBase64String([Text.Encoding]::ASCII.GetBytes(("{0}:{1}" -f $user,$token))) function CreateJsonBody { $value = @" [ { "name": "refs/heads/$Newbranch_name", "oldObjectId": "0000000000000000000000000000000000000000", "newObjectId": "$newObjectId" } ] "@ return $value } $json = CreateJsonBody # Create new Branch $NewBranch = "$collectionurl/$project/_apis/git/repositories/$repoid/refs?api-version=5.1" Invoke-RestMethod -Uri $NewBranch -Method POST -Body $json -ContentType "application/json" -Headers @{Authorization=("Basic {0}" -f $base64AuthInfo)} More on reddit.com
🌐 r/azuredevops
7
5
October 31, 2019
curl - Creating a new branch with GitHub API and php - Stack Overflow
I'm attempting some git automation using php, curl and the GitHub API. I'm first merging a feature branch into the default (master) branch by creating a pull request and then merging that pull req... More on stackoverflow.com
🌐 stackoverflow.com
June 4, 2016
🌐
GitHub
gist.github.com › ursulacj › 36ade01fa6bd5011ea31f3f6b572834e
Create a branch on Github using API and Python · GitHub
Create a branch on Github using API and Python. GitHub Gist: instantly share code, notes, and snippets.
🌐
GitHub
gist.github.com › nottrobin › a18f9e33286f9db4b83e48af6d285e29
With Github API v3, create branch, commit a change to a file and open a pull request · GitHub
f = open("myfile.txt", "a") f.write("some file content") f.close() site.create_file( path='myfile.txt', message='Adding a file', content='some file content', branch=branch_name)
🌐
GitHub
docs.github.com › en › rest › branches › branches
REST API endpoints for branches - GitHub Docs
Sync a branch of a forked repository to keep it up-to-date with the upstream repository. This endpoint works with the following fine-grained token types: ... curl -L \ -X POST \ -H "Accept: application/vnd.github+json" \ -H "Authorization: Bearer <YOUR-TOKEN>" \ -H "X-GitHub-Api-Version: 2022-11-28" \ https://api.github.com/repos/OWNER/REPO/merge-upstream \ -d '{"branch":"main"}'
🌐
GitHub
gist.github.com › potherca › 3964930
Create a branch on Github without access to a local git repo using http://hurl.eu/ · GitHub
July 4, 2025 - 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!
Find elsewhere
🌐
GitHub
github.com › octokit › node-github › issues › 317
Is there a method to create a branch? · Issue #317 · octokit/octokit.js
January 24, 2016 - I think might be useful to have a method to create a github branch. Or I was missed some docs to read? http://stackoverflow.com/questions/9506181/github-api-create-branch Thanks.
Author   markotom
🌐
Apiary Help
help.apiary.io › tools › github-integration
GitHub Integration | Apiary Help
The GitHub Integration feature allows you to create branches and utilize workflows like GitHub Flow for managing your API design process.
🌐
GitHub
developer.github.com › v3 › git › refs
REST API endpoints for Git references - GitHub Docs
Use the REST API to interact with references in your Git database on GitHub · A Git reference (git ref) is a file that contains a Git commit SHA-1 hash. When referring to a Git commit, you can use the Git reference, which is an easy-to-remember name, rather than the hash. The Git reference can be rewritten to point to a new commit. A branch ...
🌐
Pipedream
pipedream.com › apps › dev-to › integrations › github › create-branch-with-github-api-on-new-stories-for-a-tag-from-dev-to-api-int_ozseybG
Create Branch with GitHub API on New Stories for a Tag from Dev.to API - Pipedream
Create Branch with GitHub API on New Stories for a Tag from Dev.to API. Setup the Dev.to API trigger to run a workflow which integrates with the GitHub API. Pipedream's integration platform allows you to integrate Dev.to and GitHub remarkably fast.
🌐
GitHub
github.com › topics › create-branch-api
create-branch-api · GitHub Topics · GitHub
September 13, 2019 - To associate your repository with the create-branch-api topic, visit your repo's landing page and select "manage topics."
🌐
Git Scripts
gitscripts.com › git-api-create-branch
Mastering Git: How to Use Git API Create Branch
May 13, 2025 - To create a new branch in Git, use the command `git branch <branch-name>`, where `<branch-name>` is the desired name for your new branch. ... The Git API is a set of programming instructions and standards that allows applications to interact ...
🌐
Stack Overflow
stackoverflow.com › questions › 76879083 › creating-a-new-branch-using-github-api
c# - Creating a new branch using Github API - Stack Overflow
July 7, 2022 - I am trying to create a new branch, update a file in that branch, and then create a PR all using the Git API. To create a branch, I understand that we need to use the Reference API for this, as men...
🌐
Medium
rhuanca.medium.com › como-crear-un-branch-usando-el-rest-api-de-github-c389f7a5693e
Como crear un branch usando el rest api de github | by Renan Huanca | Medium
June 9, 2019 - payload = { 'ref': 'refs/heads/feature_copy', 'sha': revision }r2 = requests.post('https://api.github.com/repos/someuser/somerepo/git/refs', headers=headers, data= json.dumps(payload))print(r2.status_code) Después de ejecutar el código anterior y si r2.status_code retorna 201 el branch feature_copy se habrá creado correctamente. Si quieres un ejemplo mas completo y funcional acá tienes ejemplo implementado con algunas cosas extra: create-branch.py
🌐
GitHub
github.com › marketplace › actions › create-branch
Create Branch - GitHub Marketplace
June 2, 2023 - Optional The name of the branch to create. Default "release-candidate". If your branch conains forward slashes (/) use the full branch reference. Instead of /long/branch/name use refs/heads/long/branch/name. It's an issue with the GitHub API https://gist.github.com/jasonrudolph/10727108
🌐
Reddit
reddit.com › r/azuredevops › create a new branch via api, objectid
r/azuredevops on Reddit: Create a new branch via API, objectID
October 31, 2019 -

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?

Top answer
1 of 3
2
Generally we can create branch based on the existing branches or commits, so the newObjectId actually is the ObjectID of the specific branch or the commit ID. We can get the ObjectID of the existing branches with the following REST API: GET https://dev.azure.com/{organization}/{project}/_apis/git/repositories/{repository_ID}/refs?api-version=5.1 And get the commits with this REST API : GET https://dev.azure.com/{organization}/{project}/_apis/git/repositories/{repository_ID}/commits?api-version=5.1 Then we can create a new branch with the specific ObjectID or CommitID: Below PowerShell script for your reference to create new branch: Param( [string]$collectionurl = "https://dev.azure.com/{organization}", [string]$project = "projectname", [string]$repoid = "62c8ce54-a7bb-4e08-8ed7-40b27831bd8b", [string]$Newbranch_name = "AG1102", [string]$newObjectId= "Existing Branch objectID or the commit ID ", [string]$user = "username", [string]$token = "password/PAT" ) # Base64-encodes the Personal Access Token (PAT) appropriately $base64AuthInfo = [Convert]::ToBase64String([Text.Encoding]::ASCII.GetBytes(("{0}:{1}" -f $user,$token))) function CreateJsonBody { $value = @" [ { "name": "refs/heads/$Newbranch_name", "oldObjectId": "0000000000000000000000000000000000000000", "newObjectId": "$newObjectId" } ] "@ return $value } $json = CreateJsonBody # Create new Branch $NewBranch = "$collectionurl/$project/_apis/git/repositories/$repoid/refs?api-version=5.1" Invoke-RestMethod -Uri $NewBranch -Method POST -Body $json -ContentType "application/json" -Headers @{Authorization=("Basic {0}" -f $base64AuthInfo)}
2 of 3
1
I can't imagine that you are expected to create a new object ID, don't think that's it. In looking at the first link in your post (the GitHub link), there is an example further down, where the user specifies the existing branch's commit id as the newObjectId: NewObjectId = originalBranch.Commit.CommitId That might be worth a try! good luck, the Azure DevOps API is a bit... squirrely.
🌐
Medium
medium.com › objectsharp › adding-branch-protection-to-your-repo-with-the-github-rest-api-and-powershell-67ee19425e40
Adding Branch Protection to your Repo with the GitHub Rest API and PowerShell | by Dave Lloyd | Centrilogic | Medium
July 27, 2022 - Just to be clear here is what each section of the JSON represents in GitHub Branch Protection. ... Checkout the documentation for all the possible scenarios. Back to the script. We need to read in our JSON file and use it to create the body of our API call.
🌐
Stack Overflow
stackoverflow.com › questions › 37634738 › creating-a-new-branch-with-github-api-and-php
curl - Creating a new branch with GitHub API and php - Stack Overflow
June 4, 2016 - I'm attempting some git automation using php, curl and the GitHub API. I'm first merging a feature branch into the default (master) branch by creating a pull request and then merging that pull req...
🌐
GitHub
github.com › marketplace › actions › create-a-branch
Create a Branch · Actions · GitHub Marketplace
This action will create a new branch from a tag. This GitHub Action (written in JavaScript) wraps the GitHub Branches API.