Skip to content

remove a remote branch (push origin ':' ) #609

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

Closed
michelebortolato opened this issue Mar 11, 2017 · 4 comments
Closed

remove a remote branch (push origin ':' ) #609

michelebortolato opened this issue Mar 11, 2017 · 4 comments

Comments

@michelebortolato
Copy link

michelebortolato commented Mar 11, 2017

Hi

I cannot find a way to perform a commend equivalent to:

git push origin :branchName

A command that delete a remote branch, can gitpython perform this using git push?

Thanks

@Byron
Copy link
Member

Byron commented Apr 9, 2017

Sorry for the late reply.
You probably found a solution by now, probably this part of the documentation would have been useful too.

@adnelson
Copy link

In the same vein, is there a way to delete a remote tag? Unfortunately I couldn't find the answer in the part of the docs that @Byron mentioned. The only way I've found is something along the lines of

tag = repo.create_tag("mytag")
repo.remotes.origin.push(":" + tag.path)

which seems kind of hacky/flimsy.

@Byron
Copy link
Member

Byron commented Jun 25, 2017

@adnelson I don't think it's hacky as the documentation specified providing additional strings as arguments to git push explicitly, just to make accessible all the functionality, known or unknown at the time of writing.
Something I would imagine could ease the pain of using a string directly is to provide a utility method which hides that fact and makes it more obvious what is going on.
In case you feel the same, I think contributing it would be easy, and appreciated as well :).

@wesleywh
Copy link

wesleywh commented Apr 4, 2019

I was looking for a code example and never found a straight forward one. This is the top results in google when I search for this so I will post my solution here:

How do delete a remote branch:

import os, shutil
from git import *

def clone_repo (remote_url, local_path, branch):                               # Clone a remote repo
    print("Cloning repo %s" % remote_url)
    if os.path.isdir(local_path) is True:
        shutil.rmtree(local_path)
    Repo.clone_from(remote_url, local_path, branch=branch)

def delete_remote_branches(remote_url, branches):
    cur_dir = os.path.dirname(__file__)
    local_path="%s/%s" % (cur_dir,repo)

    clone_repo(
        remote_url=remote_url,                  # The clone url                                                           
        local_path=local_path,                  # Dir path where you want to clone to
        branch="master"                         # Branch to clone (Note: Cannot be on the same branch you want to delete)
    )
    repo = git.Repo(local_path)                 # Repo object set to cloned repo
    assert not repo.bare                        # Make sure repo isn't empty
    remote = repo.remote()                      # Set to a remote object (Defaults to 'origin') can override with name=...
    for repo_branch in repo.references:         # loop over the remote branches
        for branch in branches:                 
            if branch in repo_branch.name:      # does the branch you want to delete exist in the remote git repo? 
                print("deleting remote branch: %s" % repo_branch.remote_head)
                remote.push(refspec=(":%s" % repo_branch.remote_head)) # remote_head = the branch you want to delete Example: "origin/my-branch"

# Note: Could add some logic to delete your local cloned repo when you're done

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

No branches or pull requests

4 participants