Search code examples
python-3.xpygithub

Rename git branch name on remote using PyGithub


How can I rename github remote branch using PyGithub python API ?


Solution

  • Reference from official PyGithub - https://github.com/PyGithub/PyGithub https://pygithub.readthedocs.io/en/latest/apis.html

    from github import Github
    
    # using username and password
    g = Github("user", "password")
    
    # or using an access token
    g = Github("access_token")
    
    # get repo
    r = g.get_organization('org').get_repo('name')
    
    # Git reference of the branch that you wish to delete
    src = r.get_git_ref("heads/source branch name")
    
    # Create new branch from old branch
    r.create_git_ref("refs/heads/new branch name", sha=src.object.sha)
    
    # Delete old branch reference
    src.delete()