Pygithub: Add a Branch.delete() method

Created on 15 Oct 2019  路  3Comments  路  Source: PyGithub/PyGithub

If you're iterating through a list of branches by name, it would be handy to be able to delete them directly:

# Example 1
repo.get_branch('feature-branch-1').delete()

# Example 2
for branch in repo.get_branches():
    if some_condition:
        branch.delete()

I think the only way currently available to delete branches is to call repo.get_git_ref() to get a reference to the branch, and then call .delete() on the returned object. What I'm proposing would complement this method.

stale

Most helpful comment

If you're iterating through a list of branches by name, it would be handy to be able to delete them directly:

# Example 1
repo.get_branch('feature-branch-1').delete()

# Example 2
for branch in repo.get_branches():
    if some_condition:
        branch.delete()

I think the only way currently available to delete branches is to call repo.get_git_ref() to get a reference to the branch, and then call .delete() on the returned object. What I'm proposing would complement this method.

Please add this function, it will be very useful.

All 3 comments

This issue has been automatically marked as stale because it has not had recent activity. It will be closed if no further activity occurs. Thank you for your contributions.

If you're iterating through a list of branches by name, it would be handy to be able to delete them directly:

# Example 1
repo.get_branch('feature-branch-1').delete()

# Example 2
for branch in repo.get_branches():
    if some_condition:
        branch.delete()

I think the only way currently available to delete branches is to call repo.get_git_ref() to get a reference to the branch, and then call .delete() on the returned object. What I'm proposing would complement this method.

Please add this function, it will be very useful.

A quick workaround function to delete a branch, just in case its useful for anyone:

from github.GithubException import UnknownObjectException

def delete_branch(branch_name):
    try:
        ref = repo.get_git_ref(f"heads/{branch_name}")
        ref.delete()
    except UnknownObjectException:
        print('No such branch', branch_name)


branch_name = "br-0004"
delete_branch(branch_name)
Was this page helpful?
0 / 5 - 0 ratings