remove a remote branch (push origin ':' ) with gitpython
Asked Answered
F

3

5

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

Floccose answered 13/3, 2017 at 16:6 Comment(0)
F
6

I found the solution by myself, it is something like this:

# repo is a local repository previously checked-out
remote = repo.remote(name='origin')
remote.push(refspec=(':delete_me'))
Floccose answered 20/3, 2017 at 14:5 Comment(3)
It will delete the remote on the remote side I think, what if I want to delete the remote on local? Like git remote remove origin. A Step further I want to set-url, change the remote. Can gitypthon do this?Elan
@Elan to delete the local remote use repo.delete_remote("origin")Max
repo.remote().push()Sleeve
I
6

I was struggling with this as well so I'm posting this here for future posterity. One thing that I was missing was the need to clone down my repo first.

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
Incubus answered 4/4, 2019 at 15:42 Comment(1)
Thanks to this answer, as a plus. The branches should be like this ['origin/test', 'origin/xxx']. The origin must be contained so it could be recognized as remote branch.Sig
F
2
import git
git.Git(local_path).clone(remote_path)
repo = git.Repo(local_path)
remote = repo.remote(name='origin')
branch_attribute = repo.remotes.origin.fetch()
for branch in branch_attribute:
    rm_prefix_origin = branch.name.split('/')
    rm_prefix_origin .remove('origin')
    branch_name = '/'.join(rm_prefix_origin )    
    remote.push(refspec=(':' + branch_name​))
Farfamed answered 19/2, 2020 at 18:3 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.