[ACCEPTED]-Mercurial moving commits in another branch-mercurial

Accepted answer
Score: 54

Imagine the following scenario:

| "I want to move C and D here"


  1. hg update B
  2. hg branch "mybranch"
  3. hg commit --message "Create my branch"
  4. hg update mybranch
  5. hg graft -r C
  6. hg graft -r D
  7. hg strip -r C (this 3 should be the revision C had originally)

    The 2 strip command is provided by an extension that 1 you need to enable. You can follow a guide on how to enable it on the Mercurial Wiki.

  8. hg update default
Score: 42

A major question

Have the accidental commits reached other 9 repositories or is it just in his own? If 8 so, you can skip to the section below 'Maybe 7 the cat is still in the bag' otherwise you 6 may have a fair bit of work to do.

You are not alone

See here for 5 more discussion on how to correct the problem 4 elsewhere on Stack Overflow. What is described 3 is the 'proper' way to to it

  • export a patch
  • create the branch
  • import the patch
  • delete the earlier commits.

Maybe the cat is still in the bag

If the changes 2 are only in the local copy, then a simpler 1 solution is to

  • create the new branch
  • switch to it
  • merge the changes onto that either with your fav merge tool (go Meld) or with hg graft
  • use the hg strip command to delete the changes on the old brach
  • push the changes to the world
  • pretend like nothing ever happened, whistle a happy tune ...
Score: 8

The two answers above are both correct but, assuming one has not yet pushed the commits, there's 11 a third way.

I just successfully used the 10 rebase command to move a string of commits to 9 a topic branch I had forgotten to create 8 in the first place.

I first updated to the 7 revision from which I wanted to create the 6 branch on which my commmits were supposed 5 to be, then I rebased the earliest of my 4 commits from the wrong branch on this new 3 one and ta-da, done.

Takes more time to explain 2 it than to do it with TortoiseHg or even 1 the command line, really.

More Related questions