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


当前回答

使用Git版本2.5.0:

git branch -d `git branch --merged`

其他回答

下面创建一个别名gitcleanup,其中包含一个可选参数分支。默认情况下,它使用远程源的默认分支。已合并到此中的每个分支都将被删除。还有一个-d/-dryrun选项,用于显示将删除的内容。

安装程序

git config --global alias.cleanup '!COMMAND="git branch -D"; while [[ $# -gt 0 ]]; do case "$1" in -d|--dryrun) COMMAND="echo"; shift; ;; *) MAIN_BRANCH="$1"; shift;; esac; done; MAIN_BRANCH="${MAIN_BRANCH:-$(git symbolic-ref refs/remotes/origin/HEAD)}"; git for-each-ref --merged="$MAIN_BRANCH" --no-contains="$MAIN_BRANCH" --format="%(refname:short)" refs/heads/ | xargs -n1 -r $COMMAND;#'

用法:

git cleanup             # delete all branches that have been merged into origin/HEAD
git cleanup master2     # delete all branches that have been merged into master2
git cleanup master2 -d  # do a dryrun (show names of branches that would be delted)

可读代码

谁能读到那句“oneliner”?好了,给你

COMMAND="git branch -D";
while [[ $# -gt 0 ]]; do
  case "$1" in
    -d|--dryrun)
      COMMAND="echo"; 
      shift; 
      ;; 
    *)
      MAIN_BRANCH="$1";
      shift
    ;;
  esac;
done;
MAIN_BRANCH="${MAIN_BRANCH:-$(git symbolic-ref refs/remotes/origin/HEAD)}"; 
git for-each-ref --merged="$MAIN_BRANCH" --no-contains="$MAIN_BRANCH" --format="%(refname:short)" refs/heads/ | xargs -n1 -r $COMMAND;
#

与其他答案有什么不同?

使用--no contains选项过滤掉标识分支(所有其他被删除的分支都已合并到其中的分支),而不是grep-v(如果您特定于分支并指定refs/heads/master,则效果更好)使用远程HEAD检查默认分支名称具有一个参数,用于指定要用作合并主节点的分支具有干式运行选项

我一直在使用以下方法在一个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,你会看到一个合并的分支列表,这些分支将被清理并要求确认操作。您可以修改代码,不要求确认,但最好保留它。

在安装了gitbash的Windows上,egrep-v将无法工作

git branch --merged | grep -E -v "(master|test|dev)" | xargs git branch -d

其中grep-E-v等于egrep-v

使用-d删除已合并的分支或-D删除未合并的分支

对于我来说,gitbranch--merged不显示通过GitHub PR合并的分支。我不确定原因,但我使用以下行删除所有没有远程跟踪分支的本地分支:

diff <(git branch --format "%(refname:short)") <(git branch -r | grep -v HEAD | cut -d/ -f2-) | grep '<' | cut -c 3- | xargs git branch -D

说明:

gitbranch--格式“%(refname:short)”提供本地分支列表gitbranch-r | grep-v HEAD | cut-d/-f2-提供远程分支列表,过滤掉HEADdiff<(…)<(grep“<”筛选第一个列表中存在但第二个列表中不存在的分支cut-c 3-给出从第3个字符开始的行,从而删除前缀<xargs git branch-D针对每个分支名称执行git branch-D

或者,可以这样避免grep-v“<”:

diff --old-line-format="%L" --new-line-format="" --unchanged-line-format="" <(git branch --format "%(refname:short)") <(git branch -r | grep -v HEAD | cut -d/ -f2-) | xargs git branch -D

基于这些答案,我也制作了自己的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
}