使用gitpython删除远程分支(push origin':')

问题描述 投票:1回答:2

我找不到执行等效命令的方法:

git push origin:branchName

删除远程分支的命令,gitpython可以使用git push执行此操作吗?

谢谢

gitpython
2个回答
3
投票

我自己找到了解决方案,它是这样的:

# repo is a local repository previously checked-out
remote = repo.remote(name='origin')
remote.push(refspec=(':delete_me'))

1
投票

我也在努力解决这个问题,所以我在这里张贴这个以供将来的后代使用。我遗失的一件事是需要首先克隆我的回购。

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
© www.soinside.com 2019 - 2024. All rights reserved.