Friday Jul 11, 2008

We're in the process of transitioning the ONNV Gate to use Mercurial.  Since I'm helping drive this transition, I'm naturally getting a ton of questions about using Mercurial.


What seems to cause the most confusion for the most people?  Using Mercurial merge.  I'm going to attempt to break this down over multiple days.  Hopefully, that separation will help folks assimilate the information a little bit at a time.


So here's my first tip: if it only has one head, you don't use hg merge.  And if you have not committed your changes, then you won't have more than one head in your repository.


Sound confusing?  Don't take my advice--take Mercurial's.  It will tell you what to do, just pay attention to the command output.  When you pull in the new changeset, here's what it looks like if you need to merge:


$ hg pull -u
pulling from /home/mjnelson/work/scm/hg_merge/repo_2
searching for changes
adding changesets
adding manifests
adding file changes
added 1 changesets with 1 changes to 1 files (+1 heads)
not updating, since new heads added
(run 'hg heads' to see heads, 'hg merge' to merge)
$

...and here's what it looks like if you don't:


$ hg pull -u
pulling from /home/mjnelson/work/scm/hg_merge/repo_1
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
$

See the difference?


What happened in the above examples?  In both cases, you got a new changeset in your repository.  In the first case, since that caused your repository to grow a new head, Mercurial (wisely) said "I don't know what you want to do, please fix it yourself."  We'll save "fix it yourself" as a topic for a different day.  For now, let's talk about the second example.


In the second example, you may well have uncommitted changes in your working directory.  That's OK.  Mercurial will try to do the right thing, and it will ask you for help if it can't figure things out.  That means that, for files that are changed (or added, or deleted, or renamed) in only one place, you end up with those changes in your working directory.  For files that are changed in both places, Mercurial will attempt to merge the changes together automatically.  If it can't, because the changes conflict with each other, Mercurial will ask for your help by invoking a graphical merge program.


So that's a trivial example, and a very brief answer to the question "Why do I sometimes need to merge, but not other times?"  I don't know if I made things any clearer.  And please don't think I'm discouraging commitment--I actually prefer to commit my changes, and merge explicitly.  More on that in the future.


In the mean time, if you're interested in this topic, you would do well to follow the links above.

Comments:

Post a Comment:
Comments are closed for this entry.