How to move some changeset to a new branch in mercurial

MercurialBranchRebase

Mercurial Problem Overview


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.

Mercurial Solutions


Solution 1 - Mercurial

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.

Solution 2 - Mercurial

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

Solution 3 - Mercurial

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

Solution 4 - Mercurial

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.

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionDavid CournapeauView Question on Stackoverflow
Solution 1 - MercurialMark TolonenView Answer on Stackoverflow
Solution 2 - MercurialcrazyscotView Answer on Stackoverflow
Solution 3 - Mercurialanatoly techtonikView Answer on Stackoverflow
Solution 4 - MercurialKbiiView Answer on Stackoverflow