12

Given the SHA of a specific commit in my repo, I want to identify the branches which contain the commit, using the GitHub API.

I'm aware I can do this locally using git directly in a variety of ways, including

git branch --contains <commit>

but this is for an integrated page that's pulling data from several platforms, so I really need to be using the API.

This question had an accepted answer saying this wasn't possible back in 2013, but I'm hoping something along these lines has been added to the API since.

Is anyone aware of a way to get this information short of querying for a list of ALL commits in the branches of interest and iterating through them to compare?

Community
  • 1
  • 1
S McCrohan
  • 6,663
  • 1
  • 30
  • 39

2 Answers2

8

Could not find s.th. neither, ended up using unofficial API (I guess):

https://github.com/:user/:repo/branch_commits/:commit

You still need to scrape the response, but it's much shorter and faster. If the response just contains whitespace, there is no branch for that commit.

xiphe
  • 496
  • 6
  • 10
  • i'm also trying to use it so curious, was html response changed for you since these years or it was stable enough? – Sergey Litvinov Jun 12 '20 at 18:46
  • 1
    I honestly don't remember in which project I used this and therefore can not tell if it works. Sorry. – xiphe Jun 15 '20 at 12:23
6

None of the Repository Commit, Git Commit nor Git References endpoints provide that data, so I think the API just doesn't do it.

But the website shows that data, so if you desperately need it you could try scraping the commit's page (https://github.com/:user/:repo/commit/:sha) and getting the commit-branches div.

Finally, Github's Support is great, so you should ask them - worst-case scenario is they tell you it's not implemented, but maybe they put it on the TO-DO list.

ChrisGPT was on strike
  • 127,765
  • 105
  • 273
  • 257
mgarciaisaia
  • 14,521
  • 8
  • 57
  • 81
  • This doesn't work. When I run `git branch --contains SHA`, I see that my commit is in my `staging` branch and also in `master` when staging was later merged in there. GitHub only shows that it is in `staging`, the branch it was originally merged into. – Harvey Aug 13 '18 at 14:15