Last Updated: December 26, 2018
· 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


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 ·

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

over 1 year ago ·