Push a new branch to existing repository using REST API v3 OR v4

I want to updatate the existing github project and push it as a new branch again to that repository  so that later pull request can be created.

I want to do this via REST API v3 OR graphQL.

I couldnt able to find the way to do that.

I am not sure but I think we can use tree api end point to do that.

Any example OR approach will be helpful.

Thanks

Hi @dhananjayharel

Thanks for being here! You can do this using v3 as seen in this external post, I hope this helps!

2 Likes

Thanks.

So I can create a new branch using the API but I didnt able to find a to push my whoel projectcode into this new branch using REST API.

I think we can do it using trees API but couldnt able to find the specific example.

Hi dhananjayharel,

Using the V4 api:

First, you need to get the commit id from the branch you want to branch off of. You will also need the repository id. Example query below:

query {
  repository(owner:"JPHaus", name:"repo_name") {
    ref(qualifiedName: "refs/heads/branch-to-branch-from") {
      repository {
        id
      }
      target {
        oid
      }
    }
  }
}

The oid in the example above is the most recent commit id.

Then you need to send a mutation to create a new branch using the repo id and the commit id:

mutation {
  createRef(input: {repositoryId:"repo-id-here", name:"refs/heads/new-branch-name", oid:"oid-from-previous-query"}) {
    ref {
      id
    }
  }
}

You can test these here: https://developer.github.com/v4/explorer/

I hope this helps!

1 Like