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.
- 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!
- now create a named branch for the issue:
hg branch issueXY
optionally do:
hg commit -m "start working on issueXY"
- NOW FIX THE ISSUE (add, edit, rename, delete files) in the code and commit:
hg commit -m "fixed issueXY"
optionally add
--close-branch
- 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
- Now do the actually merging
hg merge issueXY
- … 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"