是否有一种简单的方法可以删除所有远程对等分支不再存在的跟踪分支?

例子:

分支机构(本地和远程)

主人原始/主起源/bug-fix-a起源/bug-fix-b起源/bug-fix-c

在本地,我只有一个主分支。现在我需要处理bug-fix-a,所以我检查它,处理它,并将更改推到远程。接下来,我对bug-fix-b做同样的操作。

分支机构(本地和远程)

主人bug-fix-abug-fix-b型原始/主起源/bug-fix-a起源/bug-fix-b起源/bug-fix-c

现在我有本地分支机构master,bug-fix-a,bug--fix-b。主分支维护者将把我的更改合并到主分支中,并删除他已经合并的所有分支。

因此,当前状态为:

分支机构(本地和远程)

主人bug-fix-abug-fix-b型原始/主起源/bug-fix-c

现在我想调用一些命令来删除分支(在本例中为bug-fix-a、bug-fix-b),这些分支在远程存储库中不再表示。

它类似于现有命令git remote prune origin,但更类似于git local prune origin。


当前回答

删除已合并到主控形状中的所有分支,但不要尝试删除主控形状本身:

git checkout master&&git pull origin master&&git fetch-p&&git branch-d$(git branch--merged|grep master-v)

或添加别名:

alias gitcleanlocal=“git checkout master&&git pull origin master&&git fetch-p&&git branch-d$(git branch--merged|grep master-v)”

说明:

git结帐主结帐主分支

git pull-origin master确保本地分支合并了所有远程更改

gitfetch-p删除对已删除的远程分支的引用

gitbranch-d$(gitbranch-master--merged|grepmaster-v)删除已合并到master中的所有分支,但不要尝试删除master本身

其他回答

我使用这种方法是为了更好地控制。

git branch-D$(git branch|grep-v“master”|grep/v“develop”)

这是删除所有未命名的分支:master或develop。

从这里大量的其他答案中,我得出了以下结论(我认为只有2.13和更高的数字),它应该适用于任何类似UNIX的shell:

git for-each-ref --shell --format='ref=%(if:equals=[gone])%(upstream:track)%(then)%(refname)%(end)' refs/heads | while read entry; do eval "$entry"; [ ! -z "$ref" ] && git update-ref -d "$ref" && echo "deleted $ref"; done

这特别地使用了每个ref而不是branch(因为branch是一个为人类可读输出而设计的“瓷器”命令,而不是机器处理),并使用其--shell参数来获得正确转义的输出(这使我们不必担心ref名称中的任何字符)。

在大多数其他解决方案中,模式匹配“gone”对我来说有点可怕。为了更安全,这使用--format标志来提取每个分支的上游跟踪状态。

我需要一个Windows友好的版本,所以这会删除所有使用Powershell列出为“已删除”的分支:

git branch --list --format "%(if:equals=[gone])%(upstream:track)%(then)%(refname:short)%(end)" | 
    ? { $_ -ne "" } | 
    % { git branch -D $_ }

第一行列出了上游分支已“消失”的本地分支的名称。下一行删除空行(这是未“消失”的分支的输出),然后将分支名称传递给命令以删除分支。

我已经用GitPython编写了一个Python脚本来删除远程不存在的本地分支。

    import git
    import subprocess
    from git.exc import GitCommandError
    import os

    def delete_merged_branches():
        current_dir = input("Enter repository directory:")
        repo = git.Repo(current_dir)
        git_command = git.Git(current_dir)

        # fetch the remote with prune, this will delete the remote references in local.
        for remote in repo.remotes:
            remote.fetch(prune=True)
        
        local_branches = [branch.name for branch in repo.branches]
        deleted_branches = []

        # deleted_branches are the branches which are deleted on remote but exists on local.
        for branch in local_branches:
            try:
                remote_name = 'origin/'+ branch
                repo.git.checkout(remote_name)
            except GitCommandError:
            # if the remote reference is not present, it means the branch is deleted on remote.
                deleted_branches.append(branch)
            
        for branch in deleted_branches:
            print("Deleting branch:"+branch)
            git_command.execute(["git", "branch", "-D",branch])

    
        # clean up the work flow.
        repo.git.checkout('master')
        repo.git.pull()

    if __name__ == '__main__':
        delete_merged_branches()

希望有人觉得它有用,如果我错过了什么,请添加评论。

对于一些简单的单行清除除master和develop之外的所有本地分支可能有用

git branch | grep -v "master" | grep -v "develop" | xargs git branch -D