Seems like it\'s missing from the \"Repos\" docs for v1, v2, and v3...how do I create a branch using the Github API?
Struggled to do this for Private repos thus answering the question for same cases:
For private repos you need to have the requests authenticated via "username" & "password" or OAUTH. Steps below;
Find the Personal access token for your account in github developer settings.
Make an authenticated GET request with base branch name.
From the response of GET request filter the commit SHA using jq
.
Post the new branch name and commit SHA as body in POST request to github.
In Action:
TOKEN=
Previous_branch_name=ABC
New_branch_name=XYZ
SHA=$(curl -H "Authorization: token $TOKEN" https://api.github.com/repos//git/refs/heads/$Previous_branch_name | jq -r '.object.sha')
curl -X POST -H "Authorization: token $TOKEN" \
-d "{\"ref\": \"refs/heads/$New_branch_name\",\"sha\": \"$SHA\"}" \ "https://api.github.com/repos//git/refs"