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


当前回答

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帮助。

其他回答

我的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

请尝试以下命令:

git branch -d $(git branch --merged | grep -vw $(git rev-parse --abbrev-ref HEAD))

通过使用gitrevparse,将获取当前分支名称以排除它。如果出现错误,则意味着没有要删除的本地分支。

要对远程分支执行相同操作(使用远程名称更改源),请尝试:

git push origin -vd $(git branch -r --merged | grep -vw $(git rev-parse --abbrev-ref HEAD) | cut -d/ -f2)

如果您有多个遥控器,请在剪切之前添加grep-origin |,以仅过滤源。

如果上述命令失败,请尝试先删除合并的远程跟踪分支:

git branch -rd $(git branch -r --merged | grep -vw $(git rev-parse --abbrev-ref HEAD))

然后git再次获取远程,并再次使用前面的gitpush-vd命令。

如果您经常使用它,请考虑将别名添加到~/.gitconfig文件中。

如果您错误地删除了一些分支,请使用gitreflog查找丢失的提交。

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

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

您需要从这些命令中排除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

基于这些答案,我也制作了自己的Bash脚本来实现这一点!

它使用gitbranch--merged和gitbranch-d删除已合并的分支,并在删除之前提示您输入每个分支。

merged_branches () {
    local current_branch=$(git rev-parse --abbrev-ref HEAD)
    for branch in $(git branch --merged | cut -c3-)
      do
        echo "Branch $branch is already merged into $current_branch."
        echo "Would you like to delete it? [Y]es/[N]o "
        read REPLY
        if [[ $REPLY =~ ^[Yy] ]]; then
            git branch -d $branch
        fi
    done
}