59

I want to move a changeset from one branch to another. Basically, I currently have:

A -> B -> C -> D # default branch

And I want:

A # default branch
 \-> B -> C -> D # some_new_branch

Where some_new_branch does not exist yet. I am used to git, so I guess there is a simple "mercurial" way I am missing.

3

4 Answers 4

70

One way is to export a patch for B,C,D; update to A; branch; apply patch:

hg export -o patch B C D
hg update A
hg branch branchname
hg import patch

To remove B,C,D from the default branch, use the mq extension's strip command.

5
  • In a project we decided not to include a feature in the next release, which was already part of the default branch - I used your solution to create a new branch with the changesets related to this feature. Instead of stripping the changesets, I reverted the default branch to the state before the implementation started. Commented Oct 18, 2012 at 9:59
  • This is for before those have been pushed to default, I assume?
    – NoBugs
    Commented Jan 1, 2014 at 6:46
  • Yes, any editing of history is too late if shared the original history has been pushed. You'd have to edit all copies of the history, which may not be practical or possible. Commented Jan 1, 2014 at 18:04
  • 1
    I might have used to strip extension wrong, but when I tried to hg strip B I also lost the newly created branch information from the imported commits and they ended back on default. It helped when I stripped before making a new branch and importing.
    – kerma
    Commented Aug 5, 2016 at 9:13
  • Works, thanks! If you have more revisions for the export is better to use a syntax like this: hg export -o patch -r B:D
    – quick
    Commented Aug 18, 2017 at 9:16
11

Sounds a bit like a cherry-pick operation in git. The Transplant Extension may be what you're looking for.

8

With Mercurial Queue:

# mark revisions as draft in case they were already shared
#hg phase --draft --force B:D
# make changesets a patch queue commits
# (patches are stored .hg/patches)
hg qimport -r B:D
# pop changesets from current branch
hg qpop -a
# 
hg branch some_new_branch
# push changesets to new branch
hg qpush -a
# and make them commits
hg qfinish -a

Without comments:

hg qimport -r B:D
hg qpop -a
hg branch some_new_branch
hg qpush -a
hg qfinish -a
1
  • You still need to strip moved changesets, if for example they were published on Bitbucket (in its webinterface in "Settings" -> "Strip changesets"). Other case, they will come in branch, they were moved, after first pull. Commented Jul 4, 2015 at 14:25
8

Alternative to transplant or patch, you could use graft.

hg update A
hg branch branchname
hg graft -D "B:D"
hg strip B

Note that changing history is bad practice. You should strip only if you haven't pushed yet. Otherwise, you could still backout your changes.

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.