Mercurial moving commits in another branch

Mercurial

Mercurial Problem Overview


My coworker accidentally made two commits in the default branch instead of creating new his own development branch.

How can I change this situation and moves these two commits to a new branch?

Mercurial Solutions


Solution 1 - Mercurial

Imagine the following scenario:

D
|
C
| "I want to move C and D here"
B/
|
A

Steps:

  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 should be the revision C had originally)

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

  8. hg update default

Solution 2 - Mercurial

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


You are not alone

See here for more discussion on how to correct the problem elsewhere on Stack Overflow. What is described 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 are only in the local copy, then a simpler 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 ...

Solution 3 - Mercurial

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

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

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

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

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
QuestionvoidView Question on Stackoverflow
Solution 1 - MercurialJazzView Answer on Stackoverflow
Solution 2 - MercurialChris McCauleyView Answer on Stackoverflow
Solution 3 - Mercurials.m.View Answer on Stackoverflow