Mercurial Will Beam You in The Heaven of Merging

I didn’t understood why mercurial should be fantastic and why merging is soo much easier with hg compared to subversion. This week changed my point of view.

In subversion I couldn’t keep in mind the command to copy a revision difference e.g. into the current trunk. I nearly always was forced to do a manual and error-prone merging. I am probably too stupid for that. Now that I am using hg for some of my free time projects and even partially at work I get very comfortable with hg. Especially the fast and network independent commits are awesome when working with hg. The IDE integration – in my case NetBeans 6.X – is good although working with svn/hg on the command line is faster for me.

Then, this week, I read an article about merging and I started to understand how merging works (much easier) with hg.

Lets get started. Assume your users or your QA found an issue in your current release. In hg it is easy to fix such an issue.

  1. go back to the state where you want to fix that issue or stay at the tip:
    hg update -C <oldRevisionNumber>

    Make sure you commited your changes before. Otherwise the -C won’t make backup files of changes!

  2. now create a named branch for the issue:
    hg branch issueXY

    optionally do:

    hg commit -m "start working on issueXY"
  3. NOW FIX THE ISSUE (add, edit, rename, delete files) in the code and commit:
    hg commit -m "fixed issueXY"

    optionally add

    --close-branch
  4. Go back where you want to have the fixed code. Most of the time this will be ‘default’ (svn users known as ‘trunk’) but it also could be the releaseXY branch:
    hg update default
  5. Now do the actually merging
    hg merge issueXY
  6. … and commit the merge:
    hg commit -m "merged fix of issueXY into development sources"

This is very straightforward and worked very well in practice! In my case I had to merge manually 4 files, but for that kdiff3 popped up. You can change the diff application, of course and you could also use NetBeans for that. So merging was really easy and done within 2 minutes! In the fix I changed over 25 files and mercurial even recognized refactored classes (i.e. renamed files)!

I would have never thought that merging could be that easy … until I did it myself. So, try it out! Only 6 steps into the heaven of mergurial!

Hints:

  • You can always go back to the issue via:
    hg update -C issueXY
  • Before commiting things check that you are on the correct branch:
    hg branch
  • Get all branches:
    hg branches
  • View 5 commits of the logs if glog does not work for you:
    hg log -l 5
  • The steps 4 until 6 could be applied to several branches, of course

UPDATE: check out also the comments of the reposting at dzone!

UPDATE: check out the new blog post with other merging strategies. (Although I prefer this one here)

UPDATE: If you want to try it yourself. Just follow these lines:

$ mkdir hg-test
$ cd hg-test/
/hg-test$ hg init
/hg-test$ echo -e 'Hello Work\nrelease 1.0' > hello.txt
/hg-test$ hg add hello.txt
/hg-test$ hg commit -m "initial release"

## released version. now further development

/hg-test$ echo -e 'Hello Work\ndev' > hello.txt
/hg-test$ hg commit -m "further dev"

## go back to released version to fix issue1

/hg-test$ hg update -C 0
1 Dateien aktualisiert, 0 Dateien zusammengeführt, 0 Dateien entfernt, 0 Dateien ungelöst

/hg-test$ hg branch issue1
Arbeitsverzeichnis wurde als Zweig issue1 markiert / working directory was marked as branch issue1

/hg-test$ echo -e 'Hello World\nrelease 1.0' > hello.txt
/hg-test$ hg commit -m "fixed to world"
neuer Kopf erzeugt / new head created

## go back to dev version and apply the issue

/hg-test$ hg update default
1 Dateien aktualisiert, 0 Dateien zusammengeführt, 0 Dateien entfernt, 0 Dateien ungelöst

/hg-test$ hg branches
issue1                         2:50560e696c8b
default                        1:10c88c9e32dd

/hg-test$ more hello.txt
Hello Work
dev

### now look how easy it is to apply the change! ###
### And only the atomic issue will change the code: 'Work' to 'World' ###

/hg-test$ hg merge issue1
merging hello.txt
0 Dateien aktualisiert, 1 Dateien zusammengeführt, 0 Dateien entfernt, 0 Dateien ungelöst
(Zweig-Zusammenführung, vergesse nicht 'hg commit' auszuführen)

/hg-test$ more hello.txt
Hello World
dev

/hg-test$ hg commit -m "merged issue1"
About these ads

10 thoughts on “Mercurial Will Beam You in The Heaven of Merging

  1. Having used SVN and CVS for the last couple of years, I think it’s time to try something else. But how do I choose between Git and Mercurial? I work on both Windows and Linux and anything to get met me running with little learning curve is what I need.

    Any tips?

  2. @Helen Dangote I was in the same boat but was swayed to Mecurial because 1. it is easier to use 2. it works better on windows than git (which was basically unsupported at the time). TortiseHG for windows is great.

  3. I choose hg over git simply because it feels a bit better and NetBeans support is far better. I already tried git but only for a small project where I didn’t use merging so I think git is great, too

    AND: git is developed by the linux guru .. I think you will know him ;-).

  4. I’m sure Hg is indeed better than SVN at merging, but nothing you wrote here shows its advantages. In SVN, you could branch and merge by right-clicking on a folder and selecting Branch and Merge respectively from the TortoiseSVN menu. How is Hg better in this case?

    That is what bothers me when people rave about Hg or Git. They talk about how much better these tools are at merging, and admittedly this is one of svn’s historical weak points. Though people CLAIM these tools are better at merging, they never DEMONSTRATE that assertion. I’ve had a colleague talk about how doing a big merge would be hopeless in SVN … then I fire up TortoiseSVN and do the merge effortlessly anyway. I’m open to seeing these new tools’ advantages, but I have not seen a convincing demonstration.

  5. Chris, I didn’t say that hg is better with merging than svn is. I just said: it was a lot easier – at least for me – than it was the last times I tried with svn.

    Please give me the svn commands for my example to convince me that merging with svn is doable and effortless.

  6. Thanks guys for your input. I will give both Mercurial and git a try on a little project and see how they pan out.

    But I will also keep an eye here for more discussions on what works best – saving me valuable time.

    Nice blog BTW, karussell.

  7. Pingback: Heaven of Mergurial « Find Time for the Karussell

  8. Karussell: I did give you the svn command. It just happened to be the TortoiseSVN command.

    1. Right click on folder, select TortoiseSVN, Merge
    2. Click mouse a few times
    3. Wait a bit
    4. Commit

    Is that so hard?

Comments are closed.