How can I delete all Git branches which have been merged?

后端 未结 30 1424
离开以前
离开以前 2020-11-22 14:22

I have many Git branches. How do I delete branches which have already been merged? Is there an easy way to delete them all instead of deleting them one by one?

30条回答
  •  长情又很酷
    2020-11-22 15:15

    Based on some of these answers I made my own Bash script to do it too!

    It uses git branch --merged and git branch -d to delete the branches that have been merged and prompts you for each of the branches before deleting.

    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
    }
    

提交回复
热议问题