Search code examples
pythongithubpygithub

PyGithub: How to create a branch off the master and check out?


How can I create a new branch off the master and check out the branch?

The documentation at https://pygithub.readthedocs.io/en/latest/ does not show how to do this.

Thanks a lot!!


Solution

  • I was able to do this with this code

    g = Github("user", "pass")
    repoName = "apiTest"
    source_branch = 'master'
    target_branch = 'newfeature'
    
    repo = g.get_user().get_repo(repoName)
    sb = repo.get_branch(source_branch)
    repo.create_git_ref(ref='refs/heads/' + target_branch, sha=sb.commit.sha)