Skip to content

Instantly share code, notes, and snippets.

@vanessaaleung
Created May 2, 2021 23:03
Show Gist options
  • Save vanessaaleung/a076863c7c226f36de64df01b44e4092 to your computer and use it in GitHub Desktop.
Save vanessaaleung/a076863c7c226f36de64df01b44e4092 to your computer and use it in GitHub Desktop.
create_git_branch
def create_branch(owner: str, repo: str, token: str, base: str, new_branch: str) -> None:
"""Create a new branch"""
url = "https://api.github.com/repos/{}/{}/git/refs".format(owner, repo)
headers = {
"Authorization": "token {}".format(token),
"Content-Type": "application/json"
}
try:
get_response = requests.get(url+"/heads/"+base, headers=headers)
sha = json.loads(get_response.content)["object"]["sha"]
payload = {
"ref": "refs/heads/"+new_branch,
"sha": sha
}
requests.post(url, headers=headers, json=payload)
logging.info("New branch {} created".format(new_branch))
except Exception as e:
logging.error(e)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment