hgbook

annotate en/ch08-branch.xml @ 559:b90b024729f1

WIP DocBook snapshot that all compiles. Mirabile dictu!
author Bryan O'Sullivan <bos@serpentine.com>
date Wed Feb 18 00:22:09 2009 -0800 (2009-02-18)
parents en/ch08-branch.tex@f72b7e6cbe90
children 8fcd44708f41
rev   line source
bos@559 1 <!-- vim: set filetype=docbkxml shiftwidth=2 autoindent expandtab tw=77 : -->
bos@559 2
bos@559 3 <chapter id="chap:branch">
bos@559 4 <title>Managing releases and branchy development</title>
bos@559 5
bos@559 6 <para>Mercurial provides several mechanisms for you to manage a
bos@559 7 project that is making progress on multiple fronts at once. To
bos@559 8 understand these mechanisms, let's first take a brief look at a
bos@559 9 fairly normal software project structure.</para>
bos@559 10
bos@559 11 <para>Many software projects issue periodic <quote>major</quote>
bos@559 12 releases that contain substantial new features. In parallel, they
bos@559 13 may issue <quote>minor</quote> releases. These are usually
bos@559 14 identical to the major releases off which they're based, but with
bos@559 15 a few bugs fixed.</para>
bos@559 16
bos@559 17 <para>In this chapter, we'll start by talking about how to keep
bos@559 18 records of project milestones such as releases. We'll then
bos@559 19 continue on to talk about the flow of work between different
bos@559 20 phases of a project, and how Mercurial can help you to isolate and
bos@559 21 manage this work.</para>
bos@559 22
bos@559 23 <sect1>
bos@559 24 <title>Giving a persistent name to a revision</title>
bos@559 25
bos@559 26 <para>Once you decide that you'd like to call a particular
bos@559 27 revision a <quote>release</quote>, it's a good idea to record
bos@559 28 the identity of that revision. This will let you reproduce that
bos@559 29 release at a later date, for whatever purpose you might need at
bos@559 30 the time (reproducing a bug, porting to a new platform, etc).
bos@559 31 <!-- &interaction.tag.init; --></para>
bos@559 32
bos@559 33 <para>Mercurial lets you give a permanent name to any revision
bos@559 34 using the <command role="hg-cmd">hg tag</command> command. Not
bos@559 35 surprisingly, these names are called <quote>tags</quote>. <!--
bos@559 36 &interaction.tag.tag; --></para>
bos@559 37
bos@559 38 <para>A tag is nothing more than a <quote>symbolic name</quote>
bos@559 39 for a revision. Tags exist purely for your convenience, so that
bos@559 40 you have a handy permanent way to refer to a revision; Mercurial
bos@559 41 doesn't interpret the tag names you use in any way. Neither
bos@559 42 does Mercurial place any restrictions on the name of a tag,
bos@559 43 beyond a few that are necessary to ensure that a tag can be
bos@559 44 parsed unambiguously. A tag name cannot contain any of the
bos@559 45 following characters:</para>
bos@559 46 <itemizedlist>
bos@559 47 <listitem><para>Colon (ASCII 58,
bos@559 48 <quote><literal>:</literal></quote>)</para>
bos@559 49 </listitem>
bos@559 50 <listitem><para>Carriage return (ASCII 13,
bos@559 51 <quote><literal>\r</literal></quote>)</para>
bos@559 52 </listitem>
bos@559 53 <listitem><para>Newline (ASCII 10,
bos@559 54 <quote><literal>\n</literal></quote>)</para>
bos@559 55 </listitem></itemizedlist>
bos@559 56
bos@559 57 <para>You can use the <command role="hg-cmd">hg tags</command>
bos@559 58 command to display the tags present in your repository. In the
bos@559 59 output, each tagged revision is identified first by its name,
bos@559 60 then by revision number, and finally by the unique hash of the
bos@559 61 revision. <!-- &interaction.tag.tags; --> Notice that
bos@559 62 <literal>tip</literal> is listed in the output of <command
bos@559 63 role="hg-cmd">hg tags</command>. The <literal>tip</literal>
bos@559 64 tag is a special <quote>floating</quote> tag, which always
bos@559 65 identifies the newest revision in the repository.</para>
bos@559 66
bos@559 67 <para>In the output of the <command role="hg-cmd">hg
bos@559 68 tags</command> command, tags are listed in reverse order, by
bos@559 69 revision number. This usually means that recent tags are listed
bos@559 70 before older tags. It also means that <literal>tip</literal> is
bos@559 71 always going to be the first tag listed in the output of
bos@559 72 <command role="hg-cmd">hg tags</command>.</para>
bos@559 73
bos@559 74 <para>When you run <command role="hg-cmd">hg log</command>, if it
bos@559 75 displays a revision that has tags associated with it, it will
bos@559 76 print those tags. <!-- &interaction.tag.log; --></para>
bos@559 77
bos@559 78 <para>Any time you need to provide a revision ID to a Mercurial
bos@559 79 command, the command will accept a tag name in its place.
bos@559 80 Internally, Mercurial will translate your tag name into the
bos@559 81 corresponding revision ID, then use that. <!--
bos@559 82 &interaction.tag.log.v1.0; --></para>
bos@559 83
bos@559 84 <para>There's no limit on the number of tags you can have in a
bos@559 85 repository, or on the number of tags that a single revision can
bos@559 86 have. As a practical matter, it's not a great idea to have
bos@559 87 <quote>too many</quote> (a number which will vary from project
bos@559 88 to project), simply because tags are supposed to help you to
bos@559 89 find revisions. If you have lots of tags, the ease of using
bos@559 90 them to identify revisions diminishes rapidly.</para>
bos@559 91
bos@559 92 <para>For example, if your project has milestones as frequent as
bos@559 93 every few days, it's perfectly reasonable to tag each one of
bos@559 94 those. But if you have a continuous build system that makes
bos@559 95 sure every revision can be built cleanly, you'd be introducing a
bos@559 96 lot of noise if you were to tag every clean build. Instead, you
bos@559 97 could tag failed builds (on the assumption that they're rare!),
bos@559 98 or simply not use tags to track buildability.</para>
bos@559 99
bos@559 100 <para>If you want to remove a tag that you no longer want, use
bos@559 101 <command role="hg-cmd">hg tag --remove</command>. <!--
bos@559 102 &interaction.tag.remove; --> You can also modify a tag at any
bos@559 103 time, so that it identifies a different revision, by simply
bos@559 104 issuing a new <command role="hg-cmd">hg tag</command> command.
bos@559 105 You'll have to use the <option role="hg-opt-tag">-f</option>
bos@559 106 option to tell Mercurial that you <emphasis>really</emphasis>
bos@559 107 want to update the tag. <!-- &interaction.tag.replace; --> There
bos@559 108 will still be a permanent record of the previous identity of the
bos@559 109 tag, but Mercurial will no longer use it. There's thus no
bos@559 110 penalty to tagging the wrong revision; all you have to do is
bos@559 111 turn around and tag the correct revision once you discover your
bos@559 112 error.</para>
bos@559 113
bos@559 114 <para>Mercurial stores tags in a normal revision-controlled file
bos@559 115 in your repository. If you've created any tags, you'll find
bos@559 116 them in a file named <filename
bos@559 117 role="special">.hgtags</filename>. When you run the <command
bos@559 118 role="hg-cmd">hg tag</command> command, Mercurial modifies
bos@559 119 this file, then automatically commits the change to it. This
bos@559 120 means that every time you run <command role="hg-cmd">hg
bos@559 121 tag</command>, you'll see a corresponding changeset in the
bos@559 122 output of <command role="hg-cmd">hg log</command>. <!--
bos@559 123 &interaction.tag.tip; --></para>
bos@559 124
bos@559 125 <sect2>
bos@559 126 <title>Handling tag conflicts during a merge</title>
bos@559 127
bos@559 128 <para>You won't often need to care about the <filename
bos@559 129 role="special">.hgtags</filename> file, but it sometimes
bos@559 130 makes its presence known during a merge. The format of the
bos@559 131 file is simple: it consists of a series of lines. Each line
bos@559 132 starts with a changeset hash, followed by a space, followed by
bos@559 133 the name of a tag.</para>
bos@559 134
bos@559 135 <para>If you're resolving a conflict in the <filename
bos@559 136 role="special">.hgtags</filename> file during a merge,
bos@559 137 there's one twist to modifying the <filename
bos@559 138 role="special">.hgtags</filename> file: when Mercurial is
bos@559 139 parsing the tags in a repository, it
bos@559 140 <emphasis>never</emphasis> reads the working copy of the
bos@559 141 <filename role="special">.hgtags</filename> file. Instead, it
bos@559 142 reads the <emphasis>most recently committed</emphasis>
bos@559 143 revision of the file.</para>
bos@559 144
bos@559 145 <para>An unfortunate consequence of this design is that you
bos@559 146 can't actually verify that your merged <filename
bos@559 147 role="special">.hgtags</filename> file is correct until
bos@559 148 <emphasis>after</emphasis> you've committed a change. So if
bos@559 149 you find yourself resolving a conflict on <filename
bos@559 150 role="special">.hgtags</filename> during a merge, be sure to
bos@559 151 run <command role="hg-cmd">hg tags</command> after you commit.
bos@559 152 If it finds an error in the <filename
bos@559 153 role="special">.hgtags</filename> file, it will report the
bos@559 154 location of the error, which you can then fix and commit. You
bos@559 155 should then run <command role="hg-cmd">hg tags</command>
bos@559 156 again, just to be sure that your fix is correct.</para>
bos@559 157
bos@559 158 </sect2>
bos@559 159 <sect2>
bos@559 160 <title>Tags and cloning</title>
bos@559 161
bos@559 162 <para>You may have noticed that the <command role="hg-cmd">hg
bos@559 163 clone</command> command has a <option
bos@559 164 role="hg-opt-clone">-r</option> option that lets you clone
bos@559 165 an exact copy of the repository as of a particular changeset.
bos@559 166 The new clone will not contain any project history that comes
bos@559 167 after the revision you specified. This has an interaction
bos@559 168 with tags that can surprise the unwary.</para>
bos@559 169
bos@559 170 <para>Recall that a tag is stored as a revision to the <filename
bos@559 171 role="special">.hgtags</filename> file, so that when you
bos@559 172 create a tag, the changeset in which it's recorded necessarily
bos@559 173 refers to an older changeset. When you run <command
bos@559 174 role="hg-cmd">hg clone -r foo</command> to clone a
bos@559 175 repository as of tag <literal>foo</literal>, the new clone
bos@559 176 <emphasis>will not contain the history that created the
bos@559 177 tag</emphasis> that you used to clone the repository. The
bos@559 178 result is that you'll get exactly the right subset of the
bos@559 179 project's history in the new repository, but
bos@559 180 <emphasis>not</emphasis> the tag you might have
bos@559 181 expected.</para>
bos@559 182
bos@559 183 </sect2>
bos@559 184 <sect2>
bos@559 185 <title>When permanent tags are too much</title>
bos@559 186
bos@559 187 <para>Since Mercurial's tags are revision controlled and carried
bos@559 188 around with a project's history, everyone you work with will
bos@559 189 see the tags you create. But giving names to revisions has
bos@559 190 uses beyond simply noting that revision
bos@559 191 <literal>4237e45506ee</literal> is really
bos@559 192 <literal>v2.0.2</literal>. If you're trying to track down a
bos@559 193 subtle bug, you might want a tag to remind you of something
bos@559 194 like <quote>Anne saw the symptoms with this
bos@559 195 revision</quote>.</para>
bos@559 196
bos@559 197 <para>For cases like this, what you might want to use are
bos@559 198 <emphasis>local</emphasis> tags. You can create a local tag
bos@559 199 with the <option role="hg-opt-tag">-l</option> option to the
bos@559 200 <command role="hg-cmd">hg tag</command> command. This will
bos@559 201 store the tag in a file called <filename
bos@559 202 role="special">.hg/localtags</filename>. Unlike <filename
bos@559 203 role="special">.hgtags</filename>, <filename
bos@559 204 role="special">.hg/localtags</filename> is not revision
bos@559 205 controlled. Any tags you create using <option
bos@559 206 role="hg-opt-tag">-l</option> remain strictly local to the
bos@559 207 repository you're currently working in.</para>
bos@559 208
bos@559 209 </sect2>
bos@559 210 </sect1>
bos@559 211 <sect1>
bos@559 212 <title>The flow of changes&emdash;big picture vs. little</title>
bos@559 213
bos@559 214 <para>To return to the outline I sketched at the beginning of a
bos@559 215 chapter, let's think about a project that has multiple
bos@559 216 concurrent pieces of work under development at once.</para>
bos@559 217
bos@559 218 <para>There might be a push for a new <quote>main</quote> release;
bos@559 219 a new minor bugfix release to the last main release; and an
bos@559 220 unexpected <quote>hot fix</quote> to an old release that is now
bos@559 221 in maintenance mode.</para>
bos@559 222
bos@559 223 <para>The usual way people refer to these different concurrent
bos@559 224 directions of development is as <quote>branches</quote>.
bos@559 225 However, we've already seen numerous times that Mercurial treats
bos@559 226 <emphasis>all of history</emphasis> as a series of branches and
bos@559 227 merges. Really, what we have here is two ideas that are
bos@559 228 peripherally related, but which happen to share a name.</para>
bos@559 229 <itemizedlist>
bos@559 230 <listitem><para><quote>Big picture</quote> branches represent
bos@559 231 the sweep of a project's evolution; people give them names,
bos@559 232 and talk about them in conversation.</para>
bos@559 233 </listitem>
bos@559 234 <listitem><para><quote>Little picture</quote> branches are
bos@559 235 artefacts of the day-to-day activity of developing and
bos@559 236 merging changes. They expose the narrative of how the code
bos@559 237 was developed.</para>
bos@559 238 </listitem></itemizedlist>
bos@559 239
bos@559 240 </sect1>
bos@559 241 <sect1>
bos@559 242 <title>Managing big-picture branches in repositories</title>
bos@559 243
bos@559 244 <para>The easiest way to isolate a <quote>big picture</quote>
bos@559 245 branch in Mercurial is in a dedicated repository. If you have
bos@559 246 an existing shared repository&emdash;let's call it
bos@559 247 <literal>myproject</literal>&emdash;that reaches a
bos@559 248 <quote>1.0</quote> milestone, you can start to prepare for
bos@559 249 future maintenance releases on top of version 1.0 by tagging the
bos@559 250 revision from which you prepared the 1.0 release. <!--
bos@559 251 &interaction.branch-repo.tag; --> You can then clone a new
bos@559 252 shared <literal>myproject-1.0.1</literal> repository as of that
bos@559 253 tag. <!-- &interaction.branch-repo.clone; --></para>
bos@559 254
bos@559 255 <para>Afterwards, if someone needs to work on a bug fix that ought
bos@559 256 to go into an upcoming 1.0.1 minor release, they clone the
bos@559 257 <literal>myproject-1.0.1</literal> repository, make their
bos@559 258 changes, and push them back. <!--
bos@559 259 &interaction.branch-repo.bugfix; --> Meanwhile, development for
bos@559 260 the next major release can continue, isolated and unabated, in
bos@559 261 the <literal>myproject</literal> repository. <!--
bos@559 262 &interaction.branch-repo.new; --></para>
bos@559 263
bos@559 264 </sect1>
bos@559 265 <sect1>
bos@559 266 <title>Don't repeat yourself: merging across branches</title>
bos@559 267
bos@559 268 <para>In many cases, if you have a bug to fix on a maintenance
bos@559 269 branch, the chances are good that the bug exists on your
bos@559 270 project's main branch (and possibly other maintenance branches,
bos@559 271 too). It's a rare developer who wants to fix the same bug
bos@559 272 multiple times, so let's look at a few ways that Mercurial can
bos@559 273 help you to manage these bugfixes without duplicating your
bos@559 274 work.</para>
bos@559 275
bos@559 276 <para>In the simplest instance, all you need to do is pull changes
bos@559 277 from your maintenance branch into your local clone of the target
bos@559 278 branch. <!-- &interaction.branch-repo.pull; --> You'll then need
bos@559 279 to merge the heads of the two branches, and push back to the
bos@559 280 main branch. <!-- &interaction.branch-repo.merge; --></para>
bos@559 281
bos@559 282 </sect1>
bos@559 283 <sect1>
bos@559 284 <title>Naming branches within one repository</title>
bos@559 285
bos@559 286 <para>In most instances, isolating branches in repositories is the
bos@559 287 right approach. Its simplicity makes it easy to understand; and
bos@559 288 so it's hard to make mistakes. There's a one-to-one
bos@559 289 relationship between branches you're working in and directories
bos@559 290 on your system. This lets you use normal (non-Mercurial-aware)
bos@559 291 tools to work on files within a branch/repository.</para>
bos@559 292
bos@559 293 <para>If you're more in the <quote>power user</quote> category
bos@559 294 (<emphasis>and</emphasis> your collaborators are too), there is
bos@559 295 an alternative way of handling branches that you can consider.
bos@559 296 I've already mentioned the human-level distinction between
bos@559 297 <quote>small picture</quote> and <quote>big picture</quote>
bos@559 298 branches. While Mercurial works with multiple <quote>small
bos@559 299 picture</quote> branches in a repository all the time (for
bos@559 300 example after you pull changes in, but before you merge them),
bos@559 301 it can <emphasis>also</emphasis> work with multiple <quote>big
bos@559 302 picture</quote> branches.</para>
bos@559 303
bos@559 304 <para>The key to working this way is that Mercurial lets you
bos@559 305 assign a persistent <emphasis>name</emphasis> to a branch.
bos@559 306 There always exists a branch named <literal>default</literal>.
bos@559 307 Even before you start naming branches yourself, you can find
bos@559 308 traces of the <literal>default</literal> branch if you look for
bos@559 309 them.</para>
bos@559 310
bos@559 311 <para>As an example, when you run the <command role="hg-cmd">hg
bos@559 312 commit</command> command, and it pops up your editor so that
bos@559 313 you can enter a commit message, look for a line that contains
bos@559 314 the text <quote><literal>HG: branch default</literal></quote> at
bos@559 315 the bottom. This is telling you that your commit will occur on
bos@559 316 the branch named <literal>default</literal>.</para>
bos@559 317
bos@559 318 <para>To start working with named branches, use the <command
bos@559 319 role="hg-cmd">hg branches</command> command. This command
bos@559 320 lists the named branches already present in your repository,
bos@559 321 telling you which changeset is the tip of each. <!--
bos@559 322 &interaction.branch-named.branches; --> Since you haven't
bos@559 323 created any named branches yet, the only one that exists is
bos@559 324 <literal>default</literal>.</para>
bos@559 325
bos@559 326 <para>To find out what the <quote>current</quote> branch is, run
bos@559 327 the <command role="hg-cmd">hg branch</command> command, giving
bos@559 328 it no arguments. This tells you what branch the parent of the
bos@559 329 current changeset is on. <!-- &interaction.branch-named.branch;
bos@559 330 --></para>
bos@559 331
bos@559 332 <para>To create a new branch, run the <command role="hg-cmd">hg
bos@559 333 branch</command> command again. This time, give it one
bos@559 334 argument: the name of the branch you want to create. <!--
bos@559 335 &interaction.branch-named.create; --></para>
bos@559 336
bos@559 337 <para>After you've created a branch, you might wonder what effect
bos@559 338 the <command role="hg-cmd">hg branch</command> command has had.
bos@559 339 What do the <command role="hg-cmd">hg status</command> and
bos@559 340 <command role="hg-cmd">hg tip</command> commands report? <!--
bos@559 341 &interaction.branch-named.status; --> Nothing has changed in the
bos@559 342 working directory, and there's been no new history created. As
bos@559 343 this suggests, running the <command role="hg-cmd">hg
bos@559 344 branch</command> command has no permanent effect; it only
bos@559 345 tells Mercurial what branch name to use the
bos@559 346 <emphasis>next</emphasis> time you commit a changeset.</para>
bos@559 347
bos@559 348 <para>When you commit a change, Mercurial records the name of the
bos@559 349 branch on which you committed. Once you've switched from the
bos@559 350 <literal>default</literal> branch to another and committed,
bos@559 351 you'll see the name of the new branch show up in the output of
bos@559 352 <command role="hg-cmd">hg log</command>, <command
bos@559 353 role="hg-cmd">hg tip</command>, and other commands that
bos@559 354 display the same kind of output. <!--
bos@559 355 &interaction.branch-named.commit; --> The <command
bos@559 356 role="hg-cmd">hg log</command>-like commands will print the
bos@559 357 branch name of every changeset that's not on the
bos@559 358 <literal>default</literal> branch. As a result, if you never
bos@559 359 use named branches, you'll never see this information.</para>
bos@559 360
bos@559 361 <para>Once you've named a branch and committed a change with that
bos@559 362 name, every subsequent commit that descends from that change
bos@559 363 will inherit the same branch name. You can change the name of a
bos@559 364 branch at any time, using the <command role="hg-cmd">hg
bos@559 365 branch</command> command. <!--
bos@559 366 &interaction.branch-named.rebranch; --> In practice, this is
bos@559 367 something you won't do very often, as branch names tend to have
bos@559 368 fairly long lifetimes. (This isn't a rule, just an
bos@559 369 observation.)</para>
bos@559 370
bos@559 371 </sect1>
bos@559 372 <sect1>
bos@559 373 <title>Dealing with multiple named branches in a
bos@559 374 repository</title>
bos@559 375
bos@559 376 <para>If you have more than one named branch in a repository,
bos@559 377 Mercurial will remember the branch that your working directory
bos@559 378 on when you start a command like <command role="hg-cmd">hg
bos@559 379 update</command> or <command role="hg-cmd">hg pull
bos@559 380 -u</command>. It will update the working directory to the tip
bos@559 381 of this branch, no matter what the <quote>repo-wide</quote> tip
bos@559 382 is. To update to a revision that's on a different named branch,
bos@559 383 you may need to use the <option role="hg-opt-update">-C</option>
bos@559 384 option to <command role="hg-cmd">hg update</command>.</para>
bos@559 385
bos@559 386 <para>This behaviour is a little subtle, so let's see it in
bos@559 387 action. First, let's remind ourselves what branch we're
bos@559 388 currently on, and what branches are in our repository. <!--
bos@559 389 &interaction.branch-named.parents; --> We're on the
bos@559 390 <literal>bar</literal> branch, but there also exists an older
bos@559 391 <command role="hg-cmd">hg foo</command> branch.</para>
bos@559 392
bos@559 393 <para>We can <command role="hg-cmd">hg update</command> back and
bos@559 394 forth between the tips of the <literal>foo</literal> and
bos@559 395 <literal>bar</literal> branches without needing to use the
bos@559 396 <option role="hg-opt-update">-C</option> option, because this
bos@559 397 only involves going backwards and forwards linearly through our
bos@559 398 change history. <!-- &interaction.branch-named.update-switchy;
bos@559 399 --></para>
bos@559 400
bos@559 401 <para>If we go back to the <literal>foo</literal> branch and then
bos@559 402 run <command role="hg-cmd">hg update</command>, it will keep us
bos@559 403 on <literal>foo</literal>, not move us to the tip of
bos@559 404 <literal>bar</literal>. <!--
bos@559 405 &interaction.branch-named.update-nothing; --></para>
bos@559 406
bos@559 407 <para>Committing a new change on the <literal>foo</literal> branch
bos@559 408 introduces a new head. <!--
bos@559 409 &interaction.branch-named.foo-commit; --></para>
bos@559 410
bos@559 411 </sect1>
bos@559 412 <sect1>
bos@559 413 <title>Branch names and merging</title>
bos@559 414
bos@559 415 <para>As you've probably noticed, merges in Mercurial are not
bos@559 416 symmetrical. Let's say our repository has two heads, 17 and 23.
bos@559 417 If I <command role="hg-cmd">hg update</command> to 17 and then
bos@559 418 <command role="hg-cmd">hg merge</command> with 23, Mercurial
bos@559 419 records 17 as the first parent of the merge, and 23 as the
bos@559 420 second. Whereas if I <command role="hg-cmd">hg update</command>
bos@559 421 to 23 and then <command role="hg-cmd">hg merge</command> with
bos@559 422 17, it records 23 as the first parent, and 17 as the
bos@559 423 second.</para>
bos@559 424
bos@559 425 <para>This affects Mercurial's choice of branch name when you
bos@559 426 merge. After a merge, Mercurial will retain the branch name of
bos@559 427 the first parent when you commit the result of the merge. If
bos@559 428 your first parent's branch name is <literal>foo</literal>, and
bos@559 429 you merge with <literal>bar</literal>, the branch name will
bos@559 430 still be <literal>foo</literal> after you merge.</para>
bos@559 431
bos@559 432 <para>It's not unusual for a repository to contain multiple heads,
bos@559 433 each with the same branch name. Let's say I'm working on the
bos@559 434 <literal>foo</literal> branch, and so are you. We commit
bos@559 435 different changes; I pull your changes; I now have two heads,
bos@559 436 each claiming to be on the <literal>foo</literal> branch. The
bos@559 437 result of a merge will be a single head on the
bos@559 438 <literal>foo</literal> branch, as you might hope.</para>
bos@559 439
bos@559 440 <para>But if I'm working on the <literal>bar</literal> branch, and
bos@559 441 I merge work from the <literal>foo</literal> branch, the result
bos@559 442 will remain on the <literal>bar</literal> branch. <!--
bos@559 443 &interaction.branch-named.merge; --></para>
bos@559 444
bos@559 445 <para>To give a more concrete example, if I'm working on the
bos@559 446 <literal>bleeding-edge</literal> branch, and I want to bring in
bos@559 447 the latest fixes from the <literal>stable</literal> branch,
bos@559 448 Mercurial will choose the <quote>right</quote>
bos@559 449 (<literal>bleeding-edge</literal>) branch name when I pull and
bos@559 450 merge from <literal>stable</literal>.</para>
bos@559 451
bos@559 452 </sect1>
bos@559 453 <sect1>
bos@559 454 <title>Branch naming is generally useful</title>
bos@559 455
bos@559 456 <para>You shouldn't think of named branches as applicable only to
bos@559 457 situations where you have multiple long-lived branches
bos@559 458 cohabiting in a single repository. They're very useful even in
bos@559 459 the one-branch-per-repository case.</para>
bos@559 460
bos@559 461 <para>In the simplest case, giving a name to each branch gives you
bos@559 462 a permanent record of which branch a changeset originated on.
bos@559 463 This gives you more context when you're trying to follow the
bos@559 464 history of a long-lived branchy project.</para>
bos@559 465
bos@559 466 <para>If you're working with shared repositories, you can set up a
bos@559 467 <literal role="hook">pretxnchangegroup</literal> hook on each
bos@559 468 that will block incoming changes that have the
bos@559 469 <quote>wrong</quote> branch name. This provides a simple, but
bos@559 470 effective, defence against people accidentally pushing changes
bos@559 471 from a <quote>bleeding edge</quote> branch to a
bos@559 472 <quote>stable</quote> branch. Such a hook might look like this
bos@559 473 inside the shared repo's <filename role="special">
bos@559 474 /.hgrc</filename>.</para>
bos@559 475 <programlisting>[hooks] pretxnchangegroup.branch = hg heads
bos@559 476 --template '{branches} ' | grep mybranch</programlisting>
bos@559 477
bos@559 478 </sect1>
bos@559 479 </chapter>
bos@559 480
bos@559 481 <!--
bos@559 482 local variables:
bos@559 483 sgml-parent-document: ("00book.xml" "book" "chapter")
bos@559 484 end:
bos@559 485 -->