如何删除没有本地跟踪分支的远程分支? [英] How do I delete remote branches without a local tracking branch?

查看:60
本文介绍了如何删除没有本地跟踪分支的远程分支?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何删除没有本地跟踪分支的所有远程分支?

How can I delete all remote branches without a local tracking branch?

我大约有30个没有本地分支(已删除)的远程分支,还有20个跟踪到远程的本地分支.

I have about 30 remote branches without local branches (that were deleted) and 20 local branches that track to a remote.

我很高兴找到关于如何做相反的几篇文章.

I kind find several posts on how to do the opposite.

正在寻找一种解决方案,以免我不必手动删除这些分支.

Looking for a one-liner to solve this so that I don't manually have to delete these branches one-by-one.

推荐答案

branch_not_delete=( "master" "develop")

for branch in `git for-each-ref refs/remotes/origin --format '%(refname:short)' | grep -v HEAD`;  do
    branch_name="$(gawk '{gsub("origin/", "");print}' <<< $branch)"
    local_exists="$(git rev-parse --verify $branch_name 2> /dev/null)"

    if [[ -z "${local_exists// }" ]]; then
      if ! [[ " ${branch_not_delete[*]} " == *" $branch_name "* ]]; then
        read -p "Would you like to delete $branch_name (yes/no) [no]: " yesno
        yesno=${yesno:-no}
        if [[ "$yesno" == "yes" ]]; then
          git push origin :$branch_name
        fi
      fi
    fi
done 

修改自 https://stackoverflow.com/a/38776671/5399371

已更新@torek的好建议.

Updated with @torek's nice suggestion.

这篇关于如何删除没有本地跟踪分支的远程分支?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆