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


当前回答

tl;dr:git branch--format='%(if:notequals=main)%(refname:short)%(then)%(if:notequals=master)%。

看,妈妈,没有工具!

Git可以在不使用外部工具的情况下过滤分支,这意味着有一种跨平台、相对简单的方法可以从搜索中排除默认分支。要列出合并到“main”分支中的分支,不包括“main”分行本身:git branch--format='%(if:notequals=main)%(refname:short)%(then)%(refname:short,%(end)'--merged=main。对于“master”分支:git branch--format='%(如果:notequals=master)%(refname:short)%

我们还可以将两者结合起来,仅当分支名称既不匹配“main”也不匹配“master”时才打印分支名称:git branch--format='%(if:notequals=main)%(refname:short)%(then)%

最后一个附带一个小警告:您应该首先检查默认分支(在本例中为“main”或“master”),因为--merged没有值意味着“merged into HEAD”,它可能指向您要删除的分支之一。但是尝试删除当前的本地分支无论如何都不会起作用,因此如果您正在这样做,这不会有风险。

这种方法的另一个优点是没有多余的前导空格或星号可以过滤掉。

这些命令将为排除的分支打印空行,而不是不打印任何内容,但xargs会处理这一点。如果要使用xargs以外的其他方法处理行,可能需要对空行进行特殊处理(例如通过sed'/^$/d')

有关--format的更多信息,请参阅每个ref的git帮助。

其他回答

假设我有一个名为upstream的远程和一个origin(GitHub风格,我的fork是origin,upstream是upstream)。

我不想从上游删除任何主机、HEAD或任何内容。我也不想删除开发分支,因为这是我们创建PR的公共分支。

列出所有远程分支,按合并的分支进行筛选:

git branch -r

从该列表中删除包含我知道不想删除的分支名称中的单词的行:

sed '/develop\|master\|HEAD\|upstream/d'

从引用名称中删除远程名称(origin/somebranch变为somebranch):

sed 's/.*\///'

使用xargs调用单线:

xargs git push --delete origin

把所有这些都放在一起,你会得到:

git branch -r --merged | sed '/develop\|master\|HEAD\|upstream/d' |  sed 's/.*\///' | xargs git push --delete origin

这将使我只剩下一些我曾经工作过但尚未合并的分支。然后,您可以逐个删除它们,因为它们不应该太多。

查找不再需要的分支:

git branch -ar

假设找到要删除的branch1、branch2和branch3:

git push --delete origin branch1 branch2 branch3

如果您使用的是HubFlow或GitFlow等分支模型,则可以使用此命令删除合并的功能分支:

git branch --merged | grep feature.* | grep -v "\*" | xargs -n 1 git branch -d

我的Bash脚本贡献大致基于mmrobin的答案。

它需要一些有用的参数来指定include和exclude,或者只检查/删除本地或远程分支,而不是两者。

#!/bin/bash

# exclude branches regex, configure as "(branch1|branch2|etc)$"
excludes_default="(master|next|ag/doc-updates)$"
excludes="__NOTHING__"
includes=
merged="--merged"
local=1
remote=1

while [ $# -gt 0 ]; do
  case "$1" in
  -i) shift; includes="$includes $1" ;;
  -e) shift; excludes="$1" ;;
  --no-local) local=0 ;;
  --no-remote) remote=0 ;;
  --all) merged= ;;
  *) echo "Unknown argument $1"; exit 1 ;;
  esac
  shift   # next option
done

if [ "$includes" == "" ]; then
  includes=".*"
else
  includes="($(echo $includes | sed -e 's/ /|/g'))"
fi

current_branch=$(git branch --no-color 2> /dev/null | sed -e '/^[^*]/d' -e 's/* \(.*\)/\1/')
if [ "$current_branch" != "master" ]; then
  echo "WARNING: You are on branch $current_branch, NOT master."
fi
echo -e "Fetching branches...\n"

git remote update --prune
remote_branches=$(git branch -r $merged | grep -v "/$current_branch$" | grep -v -E "$excludes" | grep -v -E "$excludes_default" | grep -E "$includes")
local_branches=$(git branch $merged | grep -v "$current_branch$" | grep -v -E "$excludes" | grep -v -E "$excludes_default" | grep -E "$includes")
if [ -z "$remote_branches" ] && [ -z "$local_branches" ]; then
  echo "No existing branches have been merged into $current_branch."
else
  echo "This will remove the following branches:"
  if [ "$remote" == 1 -a -n "$remote_branches" ]; then
    echo "$remote_branches"
  fi
  if [ "$local" == 1 -a -n "$local_branches" ]; then
    echo "$local_branches"
  fi
  read -p "Continue? (y/n): " -n 1 choice
  echo
  if [ "$choice" == "y" ] || [ "$choice" == "Y" ]; then
    if [ "$remote" == 1 ]; then
      remotes=$(git remote)
      # Remove remote branches
      for remote in $remotes
      do
        branches=$(echo "$remote_branches" | grep "$remote/" | sed "s/$remote\/\(.*\)/:\1 /g" | tr -d '\n')
        git push $remote $branches
      done
    fi

    if [ "$local" == 1 ]; then
      # Remove local branches
      locals=$(echo "$local_branches" | sed 's/origin\///g' | tr -d '\n')
      if [ -z "$locals" ]; then
        echo "No branches removed."
      else
        git branch -d $(echo "$locals" | tr -d '\n')
      fi
    fi
  fi
fi

我一直在使用以下方法在一个cmd中删除合并的本地AND远程分支。

我的bashrc文件中有以下内容:

function rmb {
  current_branch=$(git branch --no-color 2> /dev/null | sed -e '/^[^*]/d' -e 's/* \(.*\)/\1/')
  if [ "$current_branch" != "master" ]; then
    echo "WARNING: You are on branch $current_branch, NOT master."
  fi
  echo "Fetching merged branches..."
  git remote prune origin
  remote_branches=$(git branch -r --merged | grep -v '/master$' | grep -v "/$current_branch$")
  local_branches=$(git branch --merged | grep -v 'master$' | grep -v "$current_branch$")
  if [ -z "$remote_branches" ] && [ -z "$local_branches" ]; then
    echo "No existing branches have been merged into $current_branch."
  else
    echo "This will remove the following branches:"
    if [ -n "$remote_branches" ]; then
      echo "$remote_branches"
    fi
    if [ -n "$local_branches" ]; then
      echo "$local_branches"
    fi
    read -p "Continue? (y/n): " -n 1 choice
    echo
    if [ "$choice" == "y" ] || [ "$choice" == "Y" ]; then
      # Remove remote branches
      git push origin `git branch -r --merged | grep -v '/master$' | grep -v "/$current_branch$" | sed 's/origin\//:/g' | tr -d '\n'`
      # Remove local branches
      git branch -d `git branch --merged | grep -v 'master$' | grep -v "$current_branch$" | sed 's/origin\///g' | tr -d '\n'`
    else
      echo "No branches removed."
    fi
  fi
}

原始来源

这不会删除主分支,而是删除合并的本地和远程分支。一旦你在你的rc文件中有了这个,只需运行rmb,你会看到一个合并的分支列表,这些分支将被清理并要求确认操作。您可以修改代码,不要求确认,但最好保留它。

您需要从这些命令中排除master、main和develop分支。

本地git清除:

git branch --merged | grep -v '\*\|master\|main\|develop' | xargs -n 1 git branch -d

远程清零:

git branch -r --merged | grep -v '\*\|master\|main\|develop' | sed 's/origin\///' | xargs -n 1 git push --delete origin

同步远程分支的本地注册表:

git fetch -p