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

后端 未结 30 1315
离开以前
离开以前 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:18

    There is no command in Git that will do this for you automatically. But you can write a script that uses Git commands to give you what you need. This could be done in many ways depending on what branching model you are using.

    If you need to know if a branch has been merged into master the following command will yield no output if myTopicBranch has been merged (i.e. you can delete it)

    $ git rev-list master | grep $(git rev-parse myTopicBranch)
    

    You could use the Git branch command and parse out all branches in Bash and do a for loop over all branches. In this loop you check with above command if you can delete the branch or not.

提交回复
热议问题