Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Mercurial moving commits in another branch

Tags:

mercurial

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?

like image 603
void Avatar asked Jun 28 '11 13:06

void


People also ask

Can you move commits from one branch to another?

If we want to move a commit to an existing branch, we can follow a similar process using merge. In step (1) we make sure we are on the branch where we want the commit to end up. We then merge in the source branch in step (2). At this point, our target branch should have the work we want transferred.

How can I change branch in mercurial?

From the main menu, select Hg | Mercurial | Update to. In the Switch Working Directory dialog that opens, specify the target working directory: To switch to another line of development, choose Branch and select the desired branch from the list.


2 Answers

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
like image 154
Jazz Avatar answered Sep 22 '22 06:09

Jazz


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 ...
like image 35
Chris McCauley Avatar answered Sep 23 '22 06:09

Chris McCauley