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

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

    You can add the commit to the --merged option. This way you can make sure only to remove branches which are merged into i.e. the origin/master

    Following command will remove merged branches from your origin.

    git branch -r --merged origin/master | grep -v "^.*master" | sed s:origin/:: |xargs -n 1 git push origin --delete 
    

    You can test which branches will be removed replacing the git push origin --delete with echo

    git branch -r --merged origin/master | grep -v "^.*master" | sed s:origin/:: |xargs -n 1 echo
    

提交回复
热议问题