Mercurial: Switch working directory to branch without losing changes?
Asked Answered
M

5

25

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?

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.

Mickelson answered 11/10, 2011 at 16:8 Comment(0)
S
30

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.

Skilling answered 11/10, 2011 at 17:29 Comment(1)
just got abort: patch failed to apply, hmm well it only broke on one out of the three filesMedeah
M
10

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
Monterrey answered 12/10, 2011 at 9:8 Comment(3)
I've had bad experiences with shelve. It's adequate, but somehow, mistakes seem to be pretty easy to make with shelve. MQ is similar, but handles managing the patches better.Sedgewinn
For visual-oriented folks, TortoiseHG provides a UI for this workflow.Classis
@SteveHorn FYI THG shelve is not the same as the HG shelve extension. IMO the hg shelve command is far more powerful and uses built in HG merge logic. The THG feature I think was older and is not as useful.Marimaria
T
4

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

Transcribe answered 12/10, 2011 at 9:43 Comment(0)
S
2

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
Sedgewinn answered 11/10, 2011 at 16:20 Comment(4)
Is there a simple way to make a patch? I'm looking for something more lightweight/automated than copying the changed files to a temporary location, switching, and then overwritting those filesMickelson
@George: yes, mq maintains patches. It's going to look something like: hg qnew OOPSPATCH; hg qrefresh; hg qpop; hg up B1; hg qpush; <hack hack>; hg qrefresh; hg qfinish. Look into MQ.Sedgewinn
THanks, I will, I assume MQ stands for managed queues? This guy? hgbook.red-bean.com/read/…Mickelson
@George: Managed/Mercurial queues, yea. usually known as mq.Sedgewinn
P
0

All you need is simple hg up -m B1

From hg up --help:

options:  
  …  
  -m --merge      merge uncommitted changes  
  …  
Placoid answered 15/2, 2019 at 9:49 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.