How to close all merged Mercurial branches at once

Very often you end up having a messy repository with tens or even hundreds of branches which have been merged into the main branch, but not closed for some reason.

Doing that manually can be a waste of time. But, we are lazy developers, after all. Why not create a handy one-liner for that?

for branch in `hg log -r "ancestors(heads(default)) and head() and not closed()" | grep branch: | awk '{print $2}'`; do hg up -C $branch && hg commit --close-branch -m "Close branch $branch" && hg up -C default; done  

VoilĂ ! The repository is clean again.

Michael Samoylov

Python, JavaScript and Swift Expert with 12+ years of experience.

Vilnius, Lithuania

Subscribe to Michael Samoylov

Get the latest posts delivered right to your inbox.

or subscribe via RSS with Feedly!