Mercurial: Switch working directory to branch without losing changes?

I would use the shelve extension. I think it’s distributed along with TortoiseHg, you can also use it from the UI:

hg shelve --all
hg up B1
hg unshelve

Two ways. First, the obvious way:

hg diff > foo
hg up -C b1
hg import --no-commit foo
rm foo

Second, the magical way:

hg up -r 'ancestor(., b1)'  # take working dir back to the fork point
hg up b1                    # take it forward to the branch head

This way involves merges. Depending on how much your branches have diverged, this may be painless. Or it may be complicated, and you may make a mess of your changes that you haven't saved anywhere. Which is why even magicians like myself prefer to do it the first way.


Rebase extension allow you to change parent for any commit for wrongly commited changeset.

If you want just change branch for future commit - MQ (as mentioned) or Shelve


Typically for this sort of dynamic approach, I favor mercurial queues.

In your situation, what I would do would be to create a patch on default with the changes, pop the patch off, switch over to B1, and apply the patch.

It goes something like:

hg qnew OOPSPATCH
hg qrefresh 
hg qpop 
hg up B1 
hg qpush

<hack hack>

hg qrefresh 
hg qfinish