简体   繁体   中英

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.

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

Updated with @torek's nice suggestion.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM