Mercurial: Switch working directory to branch without losing changes?

Let’s say that I have a named branch ‘B1’ which I’m doing feature development on.
I am at a good stopping point before a demo though not done with the feature so I:

hg up default
hg merge B1
hg ci -m "merged in feature drop"
hg push

Now I continue working for a half an hour or so and go to commit only to realize that I forgot to update back to B1 and that my current working directory is on default – uhoh. In theory I should be able to just mark my working directory parent as the tip of B1 – is there an easy way to do this?

  • Ensuring a merge between branches happens in one direction
  • git fetch origin doesn't fetch all branches
  • Switch branch names in git
  • How should I get rid of TFS Branches properly?
  • Coder collaboration best practices using Git
  • mercurial log of changests on merged named branches, but not unmerged
  • I could of course commit, update back to B1, and merge my changes back, but then there’s an unstable changeset in default and this happens often enough to me that I would like a real solution.

  • How push directly to remote server just using push?
  • bashrc alias for not confusing git and mercurial
  • Putting a project in Eclipse under source control
  • For home projects, can Mercurial or Git (or other DVCS) provide more advantages over Subversion?
  • Why is my Git repository so much larger than Mercurial version?
  • What makes merging in DVCS easy?
  • 4 Solutions collect form web for “Mercurial: Switch working directory to branch without losing changes?”

    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.

    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

    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

    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

    Git Baby is a git and github fan, let's start git clone.