How to get all branches which contain a commit(from SHA) using GithubAPI?

Is there any way using Github API i can get all the branches which contain a specific commit.? 

2 Likes

Hey @thelayman,

Thanks for being here! I don’t think there is a more elegant way to accomplish this than the following: 

  1. Fetch all the commits for a specific branch
  2. Iterate through the commits to see if a specific commit is in the list. To do that, make a request to /repos/:owner/:repo/commits?sha=branchname, where branchname is the name of the branch you want to fetch commits for e.g. https://api.github.com/repos/andreagriffiths11/graphql-test/commits?sha=master.

I’ll dig a little deeper and see if there is any other way, 

2 Likes

@andreagriffiths11 For a repo having thousand of commits the process of iterating through will be too slow. Is there a better way to do this?

I know, Github Web UI uses an internal API to do this. for eg.
https://github.com/elastic/kibana/branch_commits/6a97f9a7ee797d302cb4f05120ff935bfe22eba0
Is that trustworthy?

I think Github should expose this as a rest API.

1 Like

@andreagriffiths11 @vishmegh88 
did you guys got any solution for this?
I am also looking for API where I can get branch details if we query with commit id. 

Hello @andreagriffiths11 

I want my branch to commit from my repository

I check with your API example

https://api.github.com/repos/andreagriffiths11/gis.utah.gov/commits?sha=metadata

it will work fine 

but when I try with my data like 

https://api.github.com/repos/DudhatShivam/drphysio/commits?sha=master

it will not work

can you guide me proper solution

If you visit https://api.github.com/repos/DudhatShivam/drphysio/commits?sha=master
you will see

{

* message: "Not Found",

* documentation_url: "https://developer.github.com/v3/repos/commits/#list-commits-on-a-repository"

}

Reason: project ‘DudhatShivam/drphysio’ does not exist on github.com (may be private, if private you should try curl -X GET -u “username:password” YOUR_URL)

  • Following documentation_url:
  • You could try: URL1/commits?sha=master
  • and URL1/commits
  • Their outputs is the same (because ?sha=master is default).

:wave: Following up from @andreagriffiths11’s example, I think it may be worth checking out this topic’s reply for an approach using the GraphQL API:

Also, this approach using the REST API might be of interest too:

1 Like