简体   繁体   English

如何在没有本地跟踪分支的情况下删除远程分支?

[英]How do I delete remote branches without a local tracking branch?

How can I delete all remote branches without a local tracking branch?如何在没有本地跟踪分支的情况下删除所有远程分支?

I have about 30 remote branches without local branches (that were deleted) and 20 local branches that track to a remote.我有大约 30 个没有本地分支(已删除)的远程分支和 20 个跟踪到远程的本地分支。

I kind find several posts on how to do the opposite.我找到了几篇关于如何做相反事情的帖子。

EDIT: 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 

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

Updated with @torek's nice suggestion.更新了@torek 的好建议。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 仅本地更改-如何删除没有相应本地分支的所有远程跟踪分支? - Local changes only - How do I delete all remote tracking branches without a corresponding local branch? 删除远程分支时如何删除本地远程跟踪分支? - How do I delete local remote-tracking branches when remote branch is deleted? 如何在没有额外提交的情况下确保我的本地分支跟踪远程相同? - How do I ensure that my local branch tracking remote is identical without additional commits? 具有多个本地跟踪分支的远程分支 - Remote branch with multiple local tracking branches GIT:删除没有本地跟踪分支的远程分支 - GIT: remove remote branches with no local tracking branch Git:如何列出跟踪不再存在的远程分支的本地分支? - Git: How do I list local branches that are tracking remote branches that no longer exist? 如何在不指定后者的情况下将本地分支重置为远程跟踪分支? - How can I reset a local branch to the remote tracking branch without specifying the latter? 用远程跟踪分支覆盖本地分支(分支已分支) - Overwrite local branch with remote tracking branch (branches have diverged) 删除分支时如何更新远程仓库的本地git跟踪 - How to update local git tracking of remote repo when it delete a branch 如何修剪远程不再存在的本地跟踪分支 - How to prune local tracking branches that do not exist on remote anymore
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM