Skip to content

Instantly share code, notes, and snippets.

@wehappyfew
Last active September 15, 2020 14:16
Show Gist options
  • Save wehappyfew/0b67c3288a7570414eef to your computer and use it in GitHub Desktop.
Save wehappyfew/0b67c3288a7570414eef to your computer and use it in GitHub Desktop.
Change the default branch of a GH repo. I use it with CodeDeploy in order to deploy different branch each time. Enjoy.
import json,requests,pprint
gh_url = "https://api.github.com"
username = 'wehappyfew'
# a token is needed for 2 Factor Auth, otherwise only Name/Pass
custom_token = 'blablablablablablabla' # a custom personal token from GitHub
repo_name = 'your_repo_name'
new_default_branch = 'something' # this is going to change every time
#encode to JSON
data = json.dumps(
{
'name': repo_name,
'default_branch': new_default_branch
}
)
# headers = {'Content-Type': 'application/json'} # may need them
# My repo was in an organization, customize the URL for yours accordingly
r = requests.patch(url = '{0}/repos/AN_ORGANIZATION_NAME/{1}'.format(gh_url, repo_name), data=data, auth=(username, custom_token))
print r.status_code
print "Default branch name:",r.json()['default_branch']
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment