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

Get the list of Git branches for a repository hosted on GitHub - Stack Overflow
Using the GitHub API: Send a GET HTTP request to https://api.github.com/repos/username/reponame/branches. More on stackoverflow.com
🌐 stackoverflow.com
November 28, 2022
Github API: get all branches from a private repo
The API returns first 30 results by default. https://docs.github.com/en/rest/reference/repos#list-branches you have to explicitly specify per_page and page if you want it differently example /repos/{owner}/{repo}/branches?per_page=100&page=1 More on reddit.com
🌐 r/github
3
1
May 28, 2021
How do I find the default branch for a repository using the Github v3 API - Stack Overflow
My goal is to get the tree for the latest SHA in the default branch GET /repos/:owner/:repo/git/trees/:sha How do I find the lastest SHA from the default branch? I know that I can call GET /repos/: More on stackoverflow.com
🌐 stackoverflow.com
GitHub API - Get the number of branches of a repo without listing all its branches - Stack Overflow
I'm currently writing a small project using the GitHub API v3. I'm constantly doing a calculation based on the number of branches a repo contains. I can't seem to find a way to do so without also More on stackoverflow.com
🌐 stackoverflow.com
🌐
GitHub
docs.github.com › en › rest › branches › branches
REST API endpoints for branches - GitHub Docs
Use the REST API to modify branches and their protection settings. This endpoint works with the following fine-grained token types: ... This endpoint can be used without authentication or the aforementioned permissions if only public resources are requested. ... curl -L \ -H "Accept: application/vnd.github+json" \ -H "Authorization: Bearer <YOUR-TOKEN>" \ -H "X-GitHub-Api-Version: 2026-03-10" \ https://api.github.com/repos/OWNER/REPO/branches
🌐
GitHub
docs.github.com › en › rest › branches › branch-protection
REST API endpoints for protected branches - GitHub Docs
Protecting a branch requires admin or owner permissions to the repository. ... Passing new arrays of users and teams replaces their previous values. ... The list of users, apps, and teams in total is limited to 100 items. This endpoint works with the following fine-grained token types: ... curl -L \ -X PUT \ -H "Accept: application/vnd.github+json" \ -H "Authorization: Bearer <YOUR-TOKEN>" \ -H "X-GitHub-Api-Version: 2026-03-10" \ https://api.github.com/repos/OWNER/REPO/branches/BRANCH/protection \ -d '{"required_status_checks":{"strict":true,"contexts":["continuous-integration/travis-ci"]},"e
🌐
GitHub
docs.github.com › en › rest › deployments › branch-policies
REST API endpoints for deployment branch policies - GitHub Docs
Creates a deployment branch or tag policy for an environment. OAuth app tokens and personal access tokens (classic) need the repo scope to use this endpoint. This endpoint works with the following fine-grained token types: ... curl -L \ -X POST ...
🌐
GitHub
developer.github.com › v3 › git › refs
REST API endpoints for Git references - GitHub Docs
[ { "ref": "refs/heads/feature-a", "node_id": "MDM6UmVmcmVmcy9oZWFkcy9mZWF0dXJlLWE=", "url": "https://api.github.com/repos/octocat/Hello-World/git/refs/heads/feature-a", "object": { "type": "commit", "sha": "aa218f56b14c9653891f9e74264a383fa43fefbd", "url": "https://api.github.com/repos/octocat/Hello-World/git/commits/aa218f56b14c9653891f9e74264a383fa43fefbd" } }, { "ref": "refs/heads/feature-b", "node_id": "MDM6UmVmcmVmcy9oZWFkcy9mZWF0dXJlLWI=", "url": "https://api.github.com/repos/octocat/Hello-World/git/refs/heads/feature-b", "object": { "type": "commit", "sha": "612077ae6dffb4d2fbd8ce0cccaa58893b07b5ac", "url": "https://api.github.com/repos/octocat/Hello-World/git/commits/612077ae6dffb4d2fbd8ce0cccaa58893b07b5ac" } } ] Returns a single reference from your Git database. The :ref in the URL must be formatted as heads/<branch name> for branches and tags/<tag name> for tags.
🌐
GitHub
gist.github.com › ursulacj › 36ade01fa6bd5011ea31f3f6b572834e
Create a branch on Github using API and Python · GitHub
Stumbled on this and it was the spring-board I needed to accomplish some automation in github I needed. Thank you for the public gist with the references. ... Thank you for writing this, it's very useful and helpful! ... Thanks for this gist! Just a typo (or a change in the api since this was written?) - the url in get_branch_sha is missing {gh_user}. The full URL should be: f'https://api.github.com/repos/{gh_user}/{repo_name}/branches/{branch_name}'
Find elsewhere
🌐
Apiary Help
help.apiary.io › tools › github-integration
GitHub Integration | Apiary Help
November 24, 2020 - The GitHub Integration feature allows you to create branches and utilize workflows like GitHub Flow for managing your API design process.
🌐
GitHub
docs.github.com › en › rest › commits › commits
REST API endpoints for commits - GitHub Docs
Returns all branches where the given commit SHA is the HEAD, or latest commit for the branch. This endpoint works with the following fine-grained token types: ... This endpoint can be used without authentication or the aforementioned permissions ...
🌐
GitHub
github.com › orgs › community › discussions › 45376
Search for a branch in Github API · community · Discussion #45376
... Hello @lucasdrpires, you can use the GitHub REST API and the GET /repos/{owner}/{repo}/branches endpoint to retrieve information about the branches within a specific repository and filter based on the branch name.
🌐
Reddit
reddit.com › r/github › github api: get all branches from a private repo
r/github on Reddit: Github API: get all branches from a private repo
May 28, 2021 -

Hey everyone,

So I was looking for a way to get a list of all the branches from a private GitHub repo. I've looked through the documentation and tried the following, but it doesn't list all the branches, there are supposed to be 100+ branches but only shows like ~20

curl -H "Authorization: token <my-token>" -X GET https://api.github.com/repos/<org>/<repo>/branches

Does anyone know why? And how I can get a list of all the branches?

I'm creating a Django webpage that lets users select a branch from a repo to deploy. Right now the user has to type in the branch, but we want to provide them with a drop-down menu to select from.

Top answer
1 of 4
10

There's no such attribute currently.

However, there's a neat trick you can use to avoid fetching all pages. If you set per_page to 1, then each page will contain 1 item and the number of pages (revealed by the last page) will also tell you the total number of items:

https://developer.github.com/v3/#pagination

So, with just one request -- you can get the total number of branches. For example, if you fetch this URL and inspect the Link header:

https://api.github.com/repos/github/linguist/branches?per_page=1

then you'll notice that the Link header is:

Link: <https://api.github.com/repositories/1725199/branches?per_page=1&page=2>; rel="next", <https://api.github.com/repositories/1725199/branches?per_page=1&page=28>; rel="last"

This tells you that there are 28 pages of results, and because there is one item per page -- the total number of branches is 28.

Hope this helps.

2 of 4
4

You can also use GraphQL API v4 to get branch count easily :

{
  repository(owner: "google", name: "gson") {
    refs(first: 0, refPrefix: "refs/heads/") {
      totalCount
    }
  }
}

Try it in the explorer

which gives :

{
  "data": {
    "repository": {
      "refs": {
        "totalCount": 13
      }
    }
  }
}

As you are doing this on multiple repo, it's also more straightforward with GraphQL as you can build the query with different aliases per repo & use only one request to get branch count for all of these :

{
  fetch: repository(owner: "github", name: "fetch") {
    ...RepoFragment
  }
  hub: repository(owner: "github", name: "hub") {
    ...RepoFragment
  }
  scientist: repository(owner: "github", name: "scientist") {
    ...RepoFragment
  }
}

fragment RepoFragment on Repository {
  refs(first: 0, refPrefix: "refs/heads/") {
    totalCount
  }
}

Try it in the explorer

🌐
GitHub
docs.github.com › en › rest › repos › rules
REST API endpoints for rules - GitHub Docs
November 28, 2022 - curl -L \ -H "Accept: application/vnd.github+json" \ -H "Authorization: Bearer <YOUR-TOKEN>" \ -H "X-GitHub-Api-Version: 2026-03-10" \ https://api.github.com/repos/OWNER/REPO/rulesets/RULESET_ID ... { "id": 42, "name": "super cool ruleset", "target": "branch", "source_type": "Repository", "source": "monalisa/my-repo", "enforcement": "active", "bypass_actors": [ { "actor_id": 234, "actor_type": "Team", "bypass_mode": "always" } ], "conditions": { "ref_name": { "include": [ "refs/heads/main", "refs/heads/master" ], "exclude": [ "refs/heads/dev*" ] } }, "rules": [ { "type": "commit_author_email_p
🌐
Stack Overflow
stackoverflow.com › questions › 76879083 › creating-a-new-branch-using-github-api
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...
Top answer
1 of 5
53

I have encountered the exact same problem. I did manage to acquire all the commits for all branches within a repository (probably not that efficient due to the API).

Approach to retrieve all commits for all branches in a repository

As you mentioned, first you gather all the branches:

# https://api.github.com/repos/:user/:repo/branches
https://api.github.com/repos/twitter/bootstrap/branches

The key that you are missing is that APIv3 for getting commits operates using a reference commit (the parameter for the API call to list commits on a repository sha). So you need to make sure when you collect the branches that you also pick up their latest sha:

Trimmed result of branch API call for twitter/bootstrap

[
  {
    "commit": {
      "url": "https://api.github.com/repos/twitter/bootstrap/commits/8b19016c3bec59acb74d95a50efce70af2117382",
      "sha": "8b19016c3bec59acb74d95a50efce70af2117382"
    },
    "name": "gh-pages"
  },
  {
    "commit": {
      "url": "https://api.github.com/repos/twitter/bootstrap/commits/d335adf644b213a5ebc9cee3f37f781ad55194ef",
      "sha": "d335adf644b213a5ebc9cee3f37f781ad55194ef"
    },
    "name": "master"
  }
]

Working with last commit's sha

So as we see the two branches here have different sha, these are the latest commit sha on those branches. What you can do now is to iterate through each branch from their latest sha:

# With sha parameter of the branch's lastest sha
# https://api.github.com/repos/:user/:repo/commits
https://api.github.com/repos/twitter/bootstrap/commits?per_page=100&sha=d335adf644b213a5ebc9cee3f37f781ad55194ef

So the above API call will list the last 100 commits of the master branch of twitter/bootstrap. Working with the API you have to specify the next commit's sha to get the next 100 commits. We can use the last commit's sha (which is 7a8d6b19767a92b1c4ea45d88d4eedc2b29bf1fa using the current example) as input for the next API call:

# Next API call for commits (use the last commit's sha)
# https://api.github.com/repos/:user/:repo/commits
https://api.github.com/repos/twitter/bootstrap/commits?per_page=100&sha=7a8d6b19767a92b1c4ea45d88d4eedc2b29bf1fa

This process is repeated until the last commit's sha is the same as the API's call sha parameter.

Next branch

That is it for one branch. Now you apply the same approach for the other branch (work from the latest sha).


There is a large issue with this approach... Since branches share some identical commits you will see the same commits over-and-over again as you move to another branch.

I can image that there is a much more efficient way to accomplish this, yet this worked for me.

2 of 5
39

I asked this same question for GitHub support, and they answered me this:

GETing /repos/:owner/:repo/commits should do the trick. You can pass the branch name in the sha parameter. For example, to get the first page of commits from the '3.0.0-wip' branch of the twitter/bootstrap repository, you would use the following curl request:

curl https://api.github.com/repos/twitter/bootstrap/commits?sha=3.0.0-wip

The docs also describe how to use pagination to get the remaining commits for this branch.

As long as you are making authenticated requests, you can make up to 5,000 requests per hour.

I used the rails github-api in my app as follows(using https://github.com/peter-murach/github gem):

github_connection = Github.new :client_id => 'your_id', :client_secret => 'your_secret', :oauth_token => 'your_oath_token'
branches_info = {}
all_branches = git_connection.repos.list_branches owner,repo_name
all_branches.body.each do |branch|
    branches_info["#{branch.name}".to_s] = "#{branch.commit.url}"
end
branches_info.keys.each do |branch|
    commits_list.push (git_connection.repos.commits.list owner,repo_name, start_date,      end_date, :sha => "branch_name")
end