I am aware that you can close a Mercurial branch with:
hg update rev-number hg commit --close-branch -m "Closing branch."
However, some of the repositories I work with a rather large, and after discovering a loose branch from years ago that I want to close, updating to it first can take many minutes (if not hours), only to do one commit and then update back to the original revision I was working from (more minutes, if not hours).
So my question, is there any way to close a Mercurial branch without updating the working directory to the branch revision 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 =
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With