Dealing with Tricky Merges

In a complicated or large project, it’s not unusual for a merge of two changesets to result in some headaches. Suppose there’s a big source file that’s been extensively edited by each side of a merge: this is almost inevitably going to result in conflicts, some of which can take a few tries to sort out.

Let’s develop a simple case of this and see how to deal with it. We’ll start off with a repository containing one file, and clone it twice.

$ hg init conflict
$ cd conflict
$ echo first > myfile.txt
$ hg ci -A -m first
adding myfile.txt
$ cd ..
$ hg clone conflict left
updating working directory
1 files updated, 0 files merged, 0 files removed, 0 files unresolved
$ hg clone conflict right
updating working directory
1 files updated, 0 files merged, 0 files removed, 0 files unresolved

In one clone, we’ll modify the file in one way.

$ cd left
$ echo left >> myfile.txt
$ hg ci -m left

In another, we’ll modify the file differently.

$ cd ../right
$ echo right >> myfile.txt
$ hg ci -m right

Next, we’ll pull each set of changes into our original repo.

$ cd ../conflict
$ hg pull -u ../left
pulling from ../left
searching for changes
adding changesets
adding manifests
adding file changes
added 1 changesets with 1 changes to 1 files
1 files updated, 0 files merged, 0 files removed, 0 files unresolved
$ hg pull -u ../right pulling from ../right searching for changes adding changesets adding manifests adding file changes added 1 changesets with 1 changes to 1 files (+1 heads) ...

Get Mercurial: The Definitive Guide now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.