How can I remove some commits in Mercurial that were applied to the wrong branch and then replay them on the correct one?
Asked Answered
S

3

12

My colleague has committed twice on his repository on the wrong branch.

What is the most effective way to take those two commits, get rid of them and then commit them correctly under the right branch? (the changes have not been pushed)

We'd ideally like a way to do this from within TortoiseHG but of course, we'll use the command-line if it is the best option.

Stonybroke answered 23/7, 2010 at 10:20 Comment(1)
This could help on the TortoiseHg side: #2963750Fayina
S
6

From what I have found out, transplant is the best extension to use. It applies a changset to any other revision you want it to.

http://mercurial.aragost.com/kick-start/tasks.html#transplanting-changes

The solution to my problem was to update to the branch where the two changes should have been applied, transplant the two changesets in and then use mq to strip the changes. All do-able within tortoisehg too.

Stonybroke answered 23/7, 2010 at 13:55 Comment(0)
W
14

The Mercurial Queues (mq) extension can help.

Given a change history that looks like this:

@  changeset:   3:9dc681b56325
|  summary:     file4
|
o  changeset:   2:6675b3f86aa7
|  summary:     file3
|
| o  changeset:   1:4a3209ed5b2f
|/   summary:     file2
|
o  changeset:   0:6ab45ac3bd6d
   summary:     file1

The following commands moves file 'file4' changeset onto the other branch (the 'file2' head):

hg qimport -r 3     // convert revision 3 to a patch
hg qpop             // remove it
hg update 1         // switch to the other branch head
hg qpush            // push the change back
hg qfin -a          // convert the applied patch back to a changeset

Resulting in:

@  changeset:   3:3faa754edb0b
|  summary:     file4
|
| o  changeset:   2:6675b3f86aa7
| |  summary:     file3
| |
o |  changeset:   1:4a3209ed5b2f
|/   summary:     file2
|
o  changeset:   0:6ab45ac3bd6d
   summary:     file1

Note that the changeset hash for rev 3 changed, due to the changeset having a different parent now. Later versions of TortoiseHg support the MQ extension as well.

Wrote answered 29/7, 2010 at 5:18 Comment(0)
S
6

From what I have found out, transplant is the best extension to use. It applies a changset to any other revision you want it to.

http://mercurial.aragost.com/kick-start/tasks.html#transplanting-changes

The solution to my problem was to update to the branch where the two changes should have been applied, transplant the two changesets in and then use mq to strip the changes. All do-able within tortoisehg too.

Stonybroke answered 23/7, 2010 at 13:55 Comment(0)
F
2

In this case it is much better to use hg rebase.

https://www.mercurial-scm.org/wiki/RebaseExtension

Fluidize answered 19/6, 2013 at 2:39 Comment(1)
This is ordinarily the simplest way to resolve this situationBarcelona

© 2022 - 2024 — McMap. All rights reserved.