vbwcow
Last Updated: February 25, 2016
·
866
· akalyaev

Remove remote braches that are merged into master

git branch -r --merged master | grep -v 'master$' | sed -e 's/origin\///' | tee | xargs -I branch -p git push origin :branch

Finds branches that have been merged into master, excludes master, replaces 'origin/' prefix, then deletes them all.

Thanks to -p option, console will always ask you for confirmation, so you do not delete branch by accident. Although it is still dangerous, so use this command with caution.

Based on @catchamonkey protip https://coderwall.com/p/oik3ta

2 Responses
Add your response

5319

It is bad practice when you have, for example, master and production branches, If you put your fixes to production branch and then merge it into master, then your script will delete production branch

over 1 year ago ·
5320

@kelion Thanks, I didn't think about it. I've added -p flag to xargs command for confirmation.

over 1 year ago ·