Change Default branch for GITHUB using API.

2020-04-28 04:48发布

问题:

I need to create a branch dev which is other than master branch. Also need to update dev to default branch using GITHUB API.

Please share details if anyone know which API to call or a way to do it, programmatically. I understand with help of UI we can do it.

Thanks Ashish

回答1:

Following the guide here: https://developer.github.com/v3/repos/#edit , default_branch input should make what you want

default_branch (string): Updates the default branch for this repository.

So, you should submit a PATCH request like:

PATCH /repos/:owner/:repo

{"default_branch": "dev"}


回答2:

I don't have enough reputation to reply to the Adam's comment above but the problem is name is a required field. The JSON should actually be:

PATCH /repos/:owner/:repo { "name":":repo" "default_branch": "dev" }



回答3:

You can use requests library:

import requests
access_token = "your_access_token"
headers = {'Authorization': f'token {access_token}', 
           'Content-Type':'application/json'}
data={"name":"knowledge-engine", "default_branch": "development"}
owner = "username"
repo_name = "repo_name"
url = f"https://api.github.com/repos/{owner}/{repo_name}"
requests.patch(url, data=json.dumps(data), headers=headers)
<Response [200]>

Docs:

  • https://help.github.com/en/articles/creating-a-personal-access-token-for-the-command-line
  • http://docs.python-requests.org/en/master/
  • https://developer.github.com/v3/repos/#edit