0

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 mentioned in the post - Github API - create branch?

So I create it using the following POST call -

/repos/:user/:repo/git/refs 

with 2 body parameters -

ref - new branch name (refs/heads/{new branch})
sha - sha1 value of the master branch.

I get a success code. However, when I use the branches API to get a list of all branches, I dont see my newly created one! The API to get branches is -

repos/:user/:repo/branches

What am I missing here?

RJP
  • 385
  • 5
  • 19
  • How many branches does this repository have? Perhaps there are too many and your new branch is on the second or last page? – knittl Aug 10 '23 at 20:34
  • You are seeing other branches, correct? – Dayton Rumbold Aug 10 '23 at 21:11
  • Are you using any tools like postman to make the requests? If not, you could try this and see if you get a different result. – Dayton Rumbold Aug 10 '23 at 21:11
  • 1
    @DaytonRumbold - Yes, I see the other branches in the response Im not using postman, I'm running this from a web app in VS. So when I make a get call to /repos/{owner}/{repo}/git/refs/head - I see my newly created reference in there. But when I make a call to the branches API - /repos/{owner}/{repo}/branches - I dont see it there. So I guess the question is, is creating a new reference the only thing needed to create a new branch? – RJP Aug 10 '23 at 21:20
  • @knittl - I have only 7 branches in my response, so its not a pagination issue. – RJP Aug 10 '23 at 21:20
  • @RJP To answer your question, I don't know if the api can do everything to create a branch from a new reference. Creating a new reference is the starting point. What the `/repos/:user/:repo/git/refs` API Call doesn't do is switch your local repository to the new branch. `git checkout` can do that. Maybe you need to enter an extra git command in order for it to complete? – Dayton Rumbold Aug 10 '23 at 21:35
  • The reason for this was in my body parameter for ref, I was sending in "ref/head/NewBranch" instead of "ref/heads/NewBranch". – RJP Aug 10 '23 at 21:48

1 Answers1

0

The reason for this was in my body parameter for ref, I was sending in "ref/head/NewBranch" instead of "ref/heads/NewBranch".

RJP
  • 385
  • 5
  • 19
  • But the question states that you sent `refs/heads/…`. VTC "not reproducible or caused by a typo" – knittl Aug 12 '23 at 18:46