36

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?

Danish
  • 3,708
  • 5
  • 29
  • 48
  • Did you create a named branch? – Lasse V. Karlsen Apr 18 '11 at 19:46
  • 2
    I think you need to revisit your problem here. I created 3 changesets on the default branch, then created a new named branch called experiment, and committed 3 changesets on that. Then I updated back to head of default, and asked Mercurial to merge with the experiment branch, and it worked, it merged, and now I can commit. Can you do a `hg summary`, followed by the `hg merge` you say isn't doing what you want it to do? – Lasse V. Karlsen Apr 18 '11 at 19:50
  • Yes, the branch has a name associated with it – Danish Apr 18 '11 at 19:51
  • @Lasse Thanks, from your comments, its seems I need to switch to make the 'trunk' branch as active before using the merge command? – Danish Apr 18 '11 at 19:53
  • 4
    You merge *into* the current branch, if you're currently on the experiment branch, Mercurial thinks you want to merge it with itself. So update to one, merge with the other. ie. `hg update trunk` and then `hg merge experiment`. Also, for future reference, you should probably use the name default (as Mercurial suggests) for your main branch, less need to be explicit about branch names in some places, default is implied. – Lasse V. Karlsen Apr 18 '11 at 19:55
  • Thanks again! My repository was actually converted from an SVN repository and the conversion process left me with a single branch called 'trunk' I would really appreciate it of your could tell me how to end up with a 'default' eventually. – Danish Apr 18 '11 at 19:57

2 Answers2

69

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.

Lasse V. Karlsen
  • 380,855
  • 102
  • 628
  • 825
16

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.

Vadim Kotov
  • 8,084
  • 8
  • 48
  • 62
dls
  • 4,146
  • 2
  • 24
  • 26