Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add a Branch.delete() method #1260

Closed
nchammas opened this issue Oct 15, 2019 · 3 comments
Closed

Add a Branch.delete() method #1260

nchammas opened this issue Oct 15, 2019 · 3 comments
Labels

Comments

@nchammas
Copy link

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
Copy link

stale bot commented Dec 14, 2019

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.

@stale stale bot added the stale label Dec 14, 2019
@stale stale bot closed this as completed Dec 21, 2019
@YuriSilenok
Copy link

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.

@abulka
Copy link

abulka commented Apr 20, 2021

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)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

3 participants