如何删除已合并的分支?我可以一次删除所有分支,而不是逐个删除每个分支吗?


当前回答

使用Git版本2.5.0:

git branch -d `git branch --merged`

其他回答

以下查询适用于我

for branch in  `git branch -r --merged | grep -v '\*\|master\|develop'|awk 'NR > 0 {print$1}'|awk '{gsub(/origin\//, "")}1'`;do git push origin --delete $branch; done

这将过滤grep管道中的任何给定分支。

在http克隆上运行良好,但在ssh连接上运行不太好。

要删除已合并到主分支的本地分支,我使用以下别名(git-config-e--global):

cleanup = "!git branch --merged master | grep -v '^*\\|master' | xargs -n 1 git branch -D"

我使用gitbranch-D来避免错误:分支“somebranch”未完全合并。消息,而我的当前签出与主分支不同。

git从git额外回购中删除合并的分支。

https://github.com/tj/git-extras/blob/master/Commands.md#git-删除合并的分支

如果要删除合并的分支,则只需删除远程跟踪分支,除非您另有说明。

因此,要删除这些分支,您可以通过

git branch--remote--合并原始/主|egrep-v“(^\*|master|development)”|cut-b 10-|xargs git push--删除原始

这将删除除主分支和开发分支外的所有合并分支(合并到主分支)。

刚刚为此创建了python脚本:

import sys
from shutil import which
import logging
from subprocess import check_output, call

logger = logging.getLogger(__name__)

if __name__ == '__main__':
    if which("git") is None:
        logger.error("git is not found!")
        sys.exit(-1)

    branches = check_output("git branch -r --merged".split()).strip().decode("utf8").splitlines()
    current = check_output("git branch --show-current".split()).strip().decode("utf8")
    blacklist = ["master", current]

    for b in branches:
        b = b.split("/")[-1]

        if b in blacklist:
            continue
        else:
            if input(f"Do you want to delete branch: '{b}' [y/n]\n").lower() == "y":
                call(f"git branch -D {b}".split())
                call(f"git push --delete origin {b}".split())