2011-08-10

Mercurial: how to completely remove a named branch

I like so much the git feature branch workflow, that in the early days of development on Python with Mercurial I created some named branches; well, that is something you should not do.

In Mercurial, the changeset contains the branch name, so you cannot develop on a separated (named) branch and then merge on default and hope that branch goes away, because it will stay.

What do I do now? Python Mercurial repository is quite big (around 200Megs) so I wanted to avoid to re-check it out. Thanks to the help of the folks on #mercurial (on freenode IRC network) I found my solution: strip the branch!

Please note that strip is dangerous. Use it only as last resort, and mind you can lose data with it. That said, it's a very powerful tool :) My main aim was to remove completely those named branches, leave no traces, and lose the changes I made on them. Another important aspect is that I didn't merged those branches on default.

So, how to get rid of a named branch:

$ hg strip "branch(${BRANCHNAME})"

and re-iterate for all the branches you have, that's it. Now, to be completely sure they were removed and no spurious changes are in the repository, you can:

$ hg pull -u
$ hg outgoing

and if it says "no changes found" you're sure that those branches are really gone.

6 comments:

Kumar Appaiah said...

s/loose/lose/g

Nice post. Thanks.

Unknown said...

I ran into similar problems shortly after the switch to hg. I now have a separate CPython sandbox (initially published on hg.python.org, now published on bitbucket), and my copies of the official repos include a 'py3k_pristine' where I never make any local modifications (I have a repo chain that goes py3k_pristine->py3k->python3.2 for pull operations, but direct from py3k to hg.python.org for push operations)

Sandro Tosi said...

@Kumar: thanks, fixed!

@Nick: I didn't have needed a local branch (or remote sandbox) for my changes yet, but that's probably what I would do. I definitely get a pristine copy of the repo, just in case, I need only a bit of bandwidth to check it out :)

Josh Triplett said...

So, as someone who heavily uses local branches in git, what does Mercurial expect people to do rather than creating branches?

Sandro Tosi said...

@Josh: first, let me clarify I'm not a mercurial expert at all :) Then, I didn't mean to say that named branches are wrong generally speaking (about mercurial), there could be use cases where they are perfectly fine, just not in the Python workflow.

There are some methods, remote sandboxes/clones (f.e. cloning the python repository on bitbucket or github), mq (an extension to mercurial) or what probably would do myself in case I need to develop a complex fix: http://docs.python.org/devguide/faq.html#i-want-to-keep-a-separate-working-copy-per-development-branch-is-it-possible

Jens Peter Secher said...

If the branch name contains a dash, you need pings:

hg strip "branch('${BRANCHNAME}')"