Can you close a Mercurial branch without updating to it first?

Yes you can, but this is not documented anywhere. I've used this technique for a long time, don't worry, it is safe.

Instead of updating, you can issue the following commands

hg debugsetparent <revision>
hg branch <branchOfRevision>

Note that the order is important. This will make your repo think it is on the new revision, while all your files are from the initial one. After that, you can use the --close-branch commit, but use the -X * option to make an empty commit.

hg commit --close-branch -X * -m "Closing branch."

Now, simply get back to your previous head, like nothing happened.

hg debugsetparent <InitialRevision>
hg branch <branchOfInitialRevision>

Finally, if you have sub-repos, you might want to temporarily rename the .hgsub file before committing the --close-branch, and rename back afterward.


Mercurial 4.8 and upwards ships with the core extension closehead which provides this functionality:

hg close-head <revision>

From the help:

This is equivalent to checking out each revision in a clean tree and running "hg commit --close-branch", except that it doesn't change the working directory.

At the moment you need to enable this extension explicitly in your hgrc:

[extensions]
closehead =