Online Book Reader

Home Category

Mercurial_ The Definitive Guide - Bryan O'Sullivan [22]

By Root 993 0
is untouched, but a new revision has been added. By referring to Figure 3-1, we can see that the changeset ID remains the same in the new repository, but the revision number has changed. (This, incidentally, is a fine example of why it’s not safe to use revision numbers when discussing changesets.) We can view the heads in a repository using the hg heads command:

$ hg heads

changeset: 6:12efb75cbece

tag: tip

parent: 4:2278160e78d4

user: Bryan O'Sullivan

date: Tue May 05 06:44:49 2009 +0000

summary: Added an extra line of output

changeset: 5:cbfc9ee6ea99

user: Bryan O'Sullivan

date: Tue May 05 06:44:52 2009 +0000

summary: A new hello for a new day.

Performing the Merge

What happens if we try to use the normal hg update command to update to the new tip?

$ hg update

abort: crosses branches (use 'hg merge' or 'hg update -C')

Mercurial is telling us that the hg update command won’t do a merge; it won’t update the working directory when it thinks we might want to do a merge, unless we force it to do so. (Incidentally, forcing the update with hg update -C would revert any uncommitted changes in the working directory.)

To start a merge between the two heads, we use the hg merge command:

$ hg merge

merging hello.c

0 files updated, 1 files merged, 0 files removed, 0 files unresolved

(branch merge, don't forget to commit)

We resolve the contents of hello.c. This updates the working directory so that it contains changes from both heads, which is reflected in both the output of hg parents and the contents of hello.c:

$ hg parents

changeset: 5:cbfc9ee6ea99

user: Bryan O'Sullivan

date: Tue May 05 06:44:52 2009 +0000

summary: A new hello for a new day.

changeset: 6:12efb75cbece

tag: tip

parent: 4:2278160e78d4

user: Bryan O'Sullivan

date: Tue May 05 06:44:49 2009 +0000

summary: Added an extra line of output

$ cat hello.c

/*

* Placed in the public domain by Bryan O'Sullivan. This program is

* not covered by patents in the United States or other countries.

*/

#include

int main(int argc, char **argv)

{

printf("once more, hello.\n");

printf("hello, world!\");

printf("hello again!\n");

return 0;

}

Committing the Results of the Merge

Whenever we’ve done a merge, hg parents will display two parents until we hg commit the results of the merge:

$ hg commit -m 'Merged changes'

We now have a new tip revision; notice that it has both of our former heads as its parents. These are the same revisions that were previously displayed by hg parents:

$ hg tip

changeset: 7:41ec93b29030

tag: tip

parent: 5:cbfc9ee6ea99

parent: 6:12efb75cbece

user: Bryan O'Sullivan

date: Tue May 05 06:44:52 2009 +0000

summary: Merged changes

In Figure 3-3, you can see a representation of what happens to the working directory during the merge, and how this affects the repository when the commit happens. During the merge, the working directory has two parent changesets, and these become the parents of the new changeset.

Figure 3-3. Working directory and repository during merge, and following commit

We sometimes talk about a merge having sides: the left side is the first parent in the output of hg parents, and the right side is the second. If the working directory was at revision 5 before we began a merge, that revision will become the left side of the merge.

Merging Conflicting Changes

Most merges are simple affairs, but sometimes you’ll find yourself merging changes where each side modifies the same portions of the same files. Unless both modifications are identical, this results in a conflict, where you have to decide how to reconcile the different changes into something coherent.

Figure 3-4. Conflicting changes to a document

Figure 3-4 illustrates an instance of two conflicting changes to a document. We started with a single version of the file; then we made some changes, while someone else made different changes to the same text. Our task in resolving

Return Main Page Previous Page Next Page

®Online Book Reader