Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to merge two branches in Mercurial when there is nothing to merge

I'm new to mercurial and I'm trying to do something really simple but can't figure out how to. I created a branch to do some experimentation without disturbing the main branch.

trunk       A -- B -- C                        \ experiment              D -- E -- F 

I like how the experiment went and want to merge it with trunk and get

trunk       A -- B -- C -- D -- E -- F 

However, since nothing was changed in the 'trunk' branch, the merge says that there is nothing to merge, which is fair enough. I just need to end up one branch called 'trunk'. How can I do that?

like image 588
Danish Avatar asked Apr 18 '11 19:04

Danish


People also ask

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.

How do I merge two branches together?

To merge branches locally, use git checkout to switch to the branch you want to merge into. This branch is typically the main branch. Next, use git merge and specify the name of the other branch to bring into this branch. This example merges the jeff/feature1 branch into the main branch.

How do I merge two branches in mercurial?

To merge two branches, you pull their heads into the same repository, update to one of them and merge the other, and then commit the result once you're happy with the merge. The resulting changeset has two parents.

How do I merge two branches on GitHub?

In GitHub Desktop, click Current Branch. Click Choose a branch to merge into BRANCH. Click the branch you want to merge into the current branch, then click Merge BRANCH into BRANCH. Note: If there are merge conflicts, GitHub Desktop will warn you above the Merge BRANCH into BRANCH button.


2 Answers

Once you've created a branch, you can't exactly achieve a single default branch (with some exceptions, see below). However, you should be able to merge experiment into default and achieve the same thing.

If you start with this:

enter image description here

and perform this:

hg update trunk hg merge experiment 

you should end up with this:

enter image description here

Other options:

Using rebase or a patch queue you could actually relocate the changesets on the experiment branch back on to default. This would basically remove the experiment named branch and create a few more default changesets. You cannot do this, however, if you've already shared the changesets form the first image, above, with another user.

like image 44
dls Avatar answered Sep 25 '22 13:09

dls


Merging in Mercurial always work the following way:

  1. Update to one of the branches (see notes below for why you want to pick one or the other and not just pick a random branch)
  2. Merge with the other branch

For instance, in your case, you would do:

hg update trunk hg merge experiment 

Choosing the right branch to update to

There are some things to consider when picking which branch to update to, and which to merge from, and it has to do with bookmarks and branch names.

Take branch names first. If you first update to the trunk branch, then merge with experiment, the merge changeset will be on the trunk branch.

However, if you update to the experiment branch, merge with trunk, then the merge changeset will be on the experiment branch.

This is important to consider when thinking about why you're merging. Are you merging the experiment into trunk, or are you updating the experiment with other changes having occured on trunk.

As for bookmarks, with newer versions of Mercurial, bookmarks are an integral part, and if you update to a bookmark, say like this:

hg update moving-target 

and then commit, that bookmark will follow your commit, ie. it will move forward.

In line of this, if you have a bookmark called moving-target on the head of the trunk branch, and update to that bookmark, the merge changeset, when you commit it, will move that bookmark forward.

like image 189
Lasse V. Karlsen Avatar answered Sep 25 '22 13:09

Lasse V. Karlsen