hgbook

annotate en/ch02-tour-basic.xml @ 553:863a82f13901

Basic progress on XML.
author Bryan O'Sullivan <bos@serpentine.com>
date Thu Feb 05 22:45:48 2009 -0800 (2009-02-05)
parents en/ch02-tour-basic.tex@f72b7e6cbe90
children 8631da51309b
rev   line source
bos@553 1 <!-- vim: set filetype=docbkxml shiftwidth=2 autoindent expandtab tw=77 : -->
bos@553 2
bos@553 3 <chapter>
bos@553 4 <title>A tour of Mercurial: the basics</title>
bos@553 5 <para>\label{chap:tour-basic}</para>
bos@553 6
bos@553 7 <sect1>
bos@553 8 <title>Installing Mercurial on your system</title>
bos@553 9 <para>\label{sec:tour:install}</para>
bos@553 10
bos@553 11 <para>Prebuilt binary packages of Mercurial are available for
bos@553 12 every popular operating system. These make it easy to start
bos@553 13 using Mercurial on your computer immediately.</para>
bos@553 14
bos@553 15 <sect2>
bos@553 16 <title>Linux</title>
bos@553 17
bos@553 18 <para>Because each Linux distribution has its own packaging
bos@553 19 tools, policies, and rate of development, it's difficult to
bos@553 20 give a comprehensive set of instructions on how to install
bos@553 21 Mercurial binaries. The version of Mercurial that you will
bos@553 22 end up with can vary depending on how active the person is who
bos@553 23 maintains the package for your distribution.</para>
bos@553 24
bos@553 25 <para>To keep things simple, I will focus on installing
bos@553 26 Mercurial from the command line under the most popular Linux
bos@553 27 distributions. Most of these distributions provide graphical
bos@553 28 package managers that will let you install Mercurial with a
bos@553 29 single click; the package name to look for is
bos@553 30 <literal>mercurial</literal>.</para>
bos@553 31
bos@553 32 <itemizedlist>
bos@553 33 <listitem><para>Debian:</para>
bos@553 34 <programlisting>apt-get install
bos@553 35 mercurial</programlisting></listitem>
bos@553 36 <listitem><para>Fedora Core:</para>
bos@553 37 <programlisting>yum install
bos@553 38 mercurial</programlisting></listitem>
bos@553 39 <listitem><para>Gentoo:</para>
bos@553 40 <programlisting>emerge mercurial</programlisting></listitem>
bos@553 41 <listitem><para>OpenSUSE:</para>
bos@553 42 <programlisting>yum install
bos@553 43 mercurial</programlisting></listitem>
bos@553 44 <listitem><para>Ubuntu: Ubuntu's Mercurial package is based on
bos@553 45 Debian's. To install it, run the following
bos@553 46 command.</para>
bos@553 47 <programlisting>apt-get install
bos@553 48 mercurial</programlisting></listitem>
bos@553 49 </itemizedlist>
bos@553 50
bos@553 51 </sect2>
bos@553 52 <sect2>
bos@553 53 <title>Solaris</title>
bos@553 54
bos@553 55 <para>SunFreeWare, at <ulink
bos@553 56 url="http://www.sunfreeware.com">http://www.sunfreeware.com</ulink>,
bos@553 57 is a good source for a large number of pre-built Solaris
bos@553 58 packages for 32 and 64 bit Intel and Sparc architectures,
bos@553 59 including current versions of Mercurial.</para>
bos@553 60
bos@553 61 </sect2>
bos@553 62 <sect2>
bos@553 63 <title>Mac OS X</title>
bos@553 64
bos@553 65 <para>Lee Cantey publishes an installer of Mercurial for Mac OS
bos@553 66 X at <ulink
bos@553 67 url="http://mercurial.berkwood.com">http://mercurial.berkwood.com</ulink>.
bos@553 68 This package works on both Intel- and Power-based Macs.
bos@553 69 Before you can use it, you must install a compatible version
bos@553 70 of Universal MacPython <citation>web:macpython</citation>.
bos@553 71 This is easy to do; simply follow the instructions on Lee's
bos@553 72 site.</para>
bos@553 73
bos@553 74 <para>It's also possible to install Mercurial using Fink or
bos@553 75 MacPorts, two popular free package managers for Mac OS X. If
bos@553 76 you have Fink, use <command>sudo apt-get install
bos@553 77 mercurial-py25</command>. If MacPorts, <command>sudo port
bos@553 78 install mercurial</command>.</para>
bos@553 79
bos@553 80 </sect2>
bos@553 81 <sect2>
bos@553 82 <title>Windows</title>
bos@553 83
bos@553 84 <para>Lee Cantey publishes an installer of Mercurial for Windows
bos@553 85 at <ulink
bos@553 86 url="http://mercurial.berkwood.com">http://mercurial.berkwood.com</ulink>.
bos@553 87 This package has no external dependencies; it <quote>just
bos@553 88 works</quote>.</para>
bos@553 89
bos@553 90 <note>
bos@553 91 <para> The Windows version of Mercurial does not
bos@553 92 automatically convert line endings between Windows and Unix
bos@553 93 styles. If you want to share work with Unix users, you must
bos@553 94 do a little additional configuration work. XXX Flesh this
bos@553 95 out.</para>
bos@553 96 </note>
bos@553 97
bos@553 98 </sect2>
bos@553 99 </sect1>
bos@553 100 <sect1>
bos@553 101 <title>Getting started</title>
bos@553 102
bos@553 103 <para>To begin, we'll use the <command role="hg-cmd">hg
bos@553 104 version</command> command to find out whether Mercurial is
bos@553 105 actually installed properly. The actual version information
bos@553 106 that it prints isn't so important; it's whether it prints
bos@553 107 anything at all that we care about. <!--
bos@553 108 &interaction.tour.version; --></para>
bos@553 109
bos@553 110 <sect2>
bos@553 111 <title>Built-in help</title>
bos@553 112
bos@553 113 <para>Mercurial provides a built-in help system. This is
bos@553 114 invaluable for those times when you find yourself stuck trying
bos@553 115 to remember how to run a command. If you are completely
bos@553 116 stuck, simply run <command role="hg-cmd">hg help</command>; it
bos@553 117 will print a brief list of commands, along with a description
bos@553 118 of what each does. If you ask for help on a specific command
bos@553 119 (as below), it prints more detailed information. <!--
bos@553 120 &interaction.tour.help; --> For a more impressive level of
bos@553 121 detail (which you won't usually need) run <command
bos@553 122 role="hg-cmd">hg help <option
bos@553 123 role="hg-opt-global">-v</option></command>. The <option
bos@553 124 role="hg-opt-global">-v</option> option is short for <option
bos@553 125 role="hg-opt-global">--verbose</option>, and tells Mercurial
bos@553 126 to print more information than it usually would.</para>
bos@553 127
bos@553 128 </sect2>
bos@553 129 </sect1>
bos@553 130 <sect1>
bos@553 131 <title>Working with a repository</title>
bos@553 132
bos@553 133 <para>In Mercurial, everything happens inside a
bos@553 134 <emphasis>repository</emphasis>. The repository for a project
bos@553 135 contains all of the files that <quote>belong to</quote> that
bos@553 136 project, along with a historical record of the project's
bos@553 137 files.</para>
bos@553 138
bos@553 139 <para>There's nothing particularly magical about a repository; it
bos@553 140 is simply a directory tree in your filesystem that Mercurial
bos@553 141 treats as special. You can rename or delete a repository any
bos@553 142 time you like, using either the command line or your file
bos@553 143 browser.</para>
bos@553 144
bos@553 145 <sect2>
bos@553 146 <title>Making a local copy of a repository</title>
bos@553 147
bos@553 148 <para><emphasis>Copying</emphasis> a repository is just a little
bos@553 149 bit special. While you could use a normal file copying
bos@553 150 command to make a copy of a repository, it's best to use a
bos@553 151 built-in command that Mercurial provides. This command is
bos@553 152 called <command role="hg-cmd">hg clone</command>, because it
bos@553 153 creates an identical copy of an existing repository. <!--
bos@553 154 &interaction.tour.clone; --> If our clone succeeded, we should
bos@553 155 now have a local directory called <filename
bos@553 156 class="directory">hello</filename>. This directory will
bos@553 157 contain some files. <!-- &interaction.tour.ls; --> These files
bos@553 158 have the same contents and history in our repository as they
bos@553 159 do in the repository we cloned.</para>
bos@553 160
bos@553 161 <para>Every Mercurial repository is complete, self-contained,
bos@553 162 and independent. It contains its own private copy of a
bos@553 163 project's files and history. A cloned repository remembers
bos@553 164 the location of the repository it was cloned from, but it does
bos@553 165 not communicate with that repository, or any other, unless you
bos@553 166 tell it to.</para>
bos@553 167
bos@553 168 <para>What this means for now is that we're free to experiment
bos@553 169 with our repository, safe in the knowledge that it's a private
bos@553 170 <quote>sandbox</quote> that won't affect anyone else.</para>
bos@553 171
bos@553 172 </sect2>
bos@553 173 <sect2>
bos@553 174 <title>What's in a repository?</title>
bos@553 175
bos@553 176 <para>When we take a more detailed look inside a repository, we
bos@553 177 can see that it contains a directory named <filename
bos@553 178 class="directory">.hg</filename>. This is where Mercurial
bos@553 179 keeps all of its metadata for the repository. <!--
bos@553 180 &interaction.tour.ls-a; --></para>
bos@553 181
bos@553 182 <para>The contents of the <filename
bos@553 183 class="directory">.hg</filename> directory and its
bos@553 184 subdirectories are private to Mercurial. Every other file and
bos@553 185 directory in the repository is yours to do with as you
bos@553 186 please.</para>
bos@553 187
bos@553 188 <para>To introduce a little terminology, the <filename
bos@553 189 class="directory">.hg</filename> directory is the
bos@553 190 <quote>real</quote> repository, and all of the files and
bos@553 191 directories that coexist with it are said to live in the
bos@553 192 <emphasis>working directory</emphasis>. An easy way to
bos@553 193 remember the distinction is that the
bos@553 194 <emphasis>repository</emphasis> contains the
bos@553 195 <emphasis>history</emphasis> of your project, while the
bos@553 196 <emphasis>working directory</emphasis> contains a
bos@553 197 <emphasis>snapshot</emphasis> of your project at a particular
bos@553 198 point in history.</para>
bos@553 199
bos@553 200 </sect2>
bos@553 201 </sect1>
bos@553 202 <sect1>
bos@553 203 <title>A tour through history</title>
bos@553 204
bos@553 205 <para>One of the first things we might want to do with a new,
bos@553 206 unfamiliar repository is understand its history. The <command
bos@553 207 role="hg-cmd">hg log</command> command gives us a view of
bos@553 208 history. <!-- &interaction.tour.log; --> By default, this
bos@553 209 command prints a brief paragraph of output for each change to
bos@553 210 the project that was recorded. In Mercurial terminology, we
bos@553 211 call each of these recorded events a
bos@553 212 <emphasis>changeset</emphasis>, because it can contain a record
bos@553 213 of changes to several files.</para>
bos@553 214
bos@553 215 <para>The fields in a record of output from <command
bos@553 216 role="hg-cmd">hg log</command> are as follows.</para>
bos@553 217 <itemizedlist>
bos@553 218 <listitem><para><literal>changeset</literal>: This field has the
bos@553 219 format of a number, followed by a colon, followed by a
bos@553 220 hexadecimal string. These are
bos@553 221 <emphasis>identifiers</emphasis> for the changeset. There
bos@553 222 are two identifiers because the number is shorter and easier
bos@553 223 to type than the hex string.</para></listitem>
bos@553 224 <listitem><para><literal>user</literal>: The identity of the
bos@553 225 person who created the changeset. This is a free-form
bos@553 226 field, but it most often contains a person's name and email
bos@553 227 address.</para></listitem>
bos@553 228 <listitem><para><literal>date</literal>: The date and time on
bos@553 229 which the changeset was created, and the timezone in which
bos@553 230 it was created. (The date and time are local to that
bos@553 231 timezone; they display what time and date it was for the
bos@553 232 person who created the changeset.)</para></listitem>
bos@553 233 <listitem><para><literal>summary</literal>: The first line of
bos@553 234 the text message that the creator of the changeset entered
bos@553 235 to describe the changeset.</para></listitem></itemizedlist>
bos@553 236 <para>The default output printed by <command role="hg-cmd">hg
bos@553 237 log</command> is purely a summary; it is missing a lot of
bos@553 238 detail.</para>
bos@553 239
bos@553 240 <para>Figure <xref id="fig:tour-basic:history"/> provides a
bos@553 241 graphical representation of the history of the <filename
bos@553 242 class="directory">hello</filename> repository, to make it a
bos@553 243 little easier to see which direction history is
bos@553 244 <quote>flowing</quote> in. We'll be returning to this figure
bos@553 245 several times in this chapter and the chapter that
bos@553 246 follows.</para>
bos@553 247
bos@553 248 <figure>
bos@553 249
bos@553 250 <para> <mediaobject><imageobject><imagedata
bos@553 251 fileref="tour-history"/></imageobject><textobject><phrase>XXX
bos@553 252 add text</phrase></textobject></mediaobject>
bos@553 253 <caption>Graphical history of the <filename
bos@553 254 class="directory">hello</filename> repository</caption>
bos@553 255 \label{fig:tour-basic:history}</para>
bos@553 256 </figure>
bos@553 257
bos@553 258 <sect2>
bos@553 259 <title>Changesets, revisions, and talking to other
bos@553 260 people</title>
bos@553 261
bos@553 262 <para>As English is a notoriously sloppy language, and computer
bos@553 263 science has a hallowed history of terminological confusion
bos@553 264 (why use one term when four will do?), revision control has a
bos@553 265 variety of words and phrases that mean the same thing. If you
bos@553 266 are talking about Mercurial history with other people, you
bos@553 267 will find that the word <quote>changeset</quote> is often
bos@553 268 compressed to <quote>change</quote> or (when written)
bos@553 269 <quote>cset</quote>, and sometimes a changeset is referred to
bos@553 270 as a <quote>revision</quote> or a <quote>rev</quote>.</para>
bos@553 271
bos@553 272 <para>While it doesn't matter what <emphasis>word</emphasis> you
bos@553 273 use to refer to the concept of <quote>a changeset</quote>, the
bos@553 274 <emphasis>identifier</emphasis> that you use to refer to
bos@553 275 <quote>a <emphasis>specific</emphasis> changeset</quote> is of
bos@553 276 great importance. Recall that the <literal>changeset</literal>
bos@553 277 field in the output from <command role="hg-cmd">hg
bos@553 278 log</command> identifies a changeset using both a number and
bos@553 279 a hexadecimal string.</para>
bos@553 280 <itemizedlist>
bos@553 281 <listitem><para>The revision number is <emphasis>only valid in
bos@553 282 that repository</emphasis>,</para></listitem>
bos@553 283 <listitem><para>while the hex string is the
bos@553 284 <emphasis>permanent, unchanging identifier</emphasis> that
bos@553 285 will always identify that exact changeset in
bos@553 286 <emphasis>every</emphasis> copy of the
bos@553 287 repository.</para></listitem></itemizedlist>
bos@553 288 <para>This distinction is important. If you send someone an
bos@553 289 email talking about <quote>revision 33</quote>, there's a high
bos@553 290 likelihood that their revision 33 will <emphasis>not be the
bos@553 291 same</emphasis> as yours. The reason for this is that a
bos@553 292 revision number depends on the order in which changes arrived
bos@553 293 in a repository, and there is no guarantee that the same
bos@553 294 changes will happen in the same order in different
bos@553 295 repositories. Three changes $a,b,c$ can easily appear in one
bos@553 296 repository as $0,1,2$, while in another as $1,0,2$.</para>
bos@553 297
bos@553 298 <para>Mercurial uses revision numbers purely as a convenient
bos@553 299 shorthand. If you need to discuss a changeset with someone,
bos@553 300 or make a record of a changeset for some other reason (for
bos@553 301 example, in a bug report), use the hexadecimal
bos@553 302 identifier.</para>
bos@553 303
bos@553 304 </sect2>
bos@553 305 <sect2>
bos@553 306 <title>Viewing specific revisions</title>
bos@553 307
bos@553 308 <para>To narrow the output of <command role="hg-cmd">hg
bos@553 309 log</command> down to a single revision, use the <option
bos@553 310 role="hg-opt-log">-r</option> (or <option
bos@553 311 role="hg-opt-log">--rev</option>) option. You can use
bos@553 312 either a revision number or a long-form changeset identifier,
bos@553 313 and you can provide as many revisions as you want. <!--
bos@553 314 &interaction.tour.log-r; --></para>
bos@553 315
bos@553 316 <para>If you want to see the history of several revisions
bos@553 317 without having to list each one, you can use <emphasis>range
bos@553 318 notation</emphasis>; this lets you express the idea <quote>I
bos@553 319 want all revisions between $a$ and $b$, inclusive</quote>.
bos@553 320 <!-- &interaction.tour.log.range; --> Mercurial also honours
bos@553 321 the order in which you specify revisions, so <command
bos@553 322 role="hg-cmd">hg log -r 2:4</command> prints $2,3,4$ while
bos@553 323 <command role="hg-cmd">hg log -r 4:2</command> prints
bos@553 324 $4,3,2$.</para>
bos@553 325
bos@553 326 </sect2>
bos@553 327 <sect2>
bos@553 328 <title>More detailed information</title>
bos@553 329
bos@553 330 <para>While the summary information printed by <command
bos@553 331 role="hg-cmd">hg log</command> is useful if you already know
bos@553 332 what you're looking for, you may need to see a complete
bos@553 333 description of the change, or a list of the files changed, if
bos@553 334 you're trying to decide whether a changeset is the one you're
bos@553 335 looking for. The <command role="hg-cmd">hg log</command>
bos@553 336 command's <option role="hg-opt-global">-v</option> (or <option
bos@553 337 role="hg-opt-global">--verbose</option>) option gives you
bos@553 338 this extra detail. <!-- &interaction.tour.log-v; --></para>
bos@553 339
bos@553 340 <para>If you want to see both the description and content of a
bos@553 341 change, add the <option role="hg-opt-log">-p</option> (or
bos@553 342 <option role="hg-opt-log">--patch</option>) option. This
bos@553 343 displays the content of a change as a <emphasis>unified
bos@553 344 diff</emphasis> (if you've never seen a unified diff before,
bos@553 345 see section <xref id="sec:mq:patch"/> for an
bos@553 346 overview). <!-- &interaction.tour.log-vp; --></para>
bos@553 347
bos@553 348 </sect2>
bos@553 349 </sect1>
bos@553 350 <sect1>
bos@553 351 <title>All about command options</title>
bos@553 352
bos@553 353 <para>Let's take a brief break from exploring Mercurial commands
bos@553 354 to discuss a pattern in the way that they work; you may find
bos@553 355 this useful to keep in mind as we continue our tour.</para>
bos@553 356
bos@553 357 <para>Mercurial has a consistent and straightforward approach to
bos@553 358 dealing with the options that you can pass to commands. It
bos@553 359 follows the conventions for options that are common to modern
bos@553 360 Linux and Unix systems.</para>
bos@553 361 <itemizedlist>
bos@553 362 <listitem><para>Every option has a long name. For example, as
bos@553 363 we've already seen, the <command role="hg-cmd">hg
bos@553 364 log</command> command accepts a <option
bos@553 365 role="hg-opt-log">--rev</option> option.</para></listitem>
bos@553 366 <listitem><para>Most options have short names, too. Instead of
bos@553 367 <option role="hg-opt-log">--rev</option>, we can use <option
bos@553 368 role="hg-opt-log">-r</option>. (The reason that some
bos@553 369 options don't have short names is that the options in
bos@553 370 question are rarely used.)</para></listitem>
bos@553 371 <listitem><para>Long options start with two dashes (e.g. <option
bos@553 372 role="hg-opt-log">--rev</option>), while short options
bos@553 373 start with one (e.g. <option
bos@553 374 role="hg-opt-log">-r</option>).</para></listitem>
bos@553 375 <listitem><para>Option naming and usage is consistent across
bos@553 376 commands. For example, every command that lets you specify
bos@553 377 a changeset ID or revision number accepts both <option
bos@553 378 role="hg-opt-log">-r</option> and <option
bos@553 379 role="hg-opt-log">--rev</option>
bos@553 380 arguments.</para></listitem></itemizedlist>
bos@553 381 <para>In the examples throughout this book, I use short options
bos@553 382 instead of long. This just reflects my own preference, so don't
bos@553 383 read anything significant into it.</para>
bos@553 384
bos@553 385 <para>Most commands that print output of some kind will print more
bos@553 386 output when passed a <option role="hg-opt-global">-v</option>
bos@553 387 (or <option role="hg-opt-global">--verbose</option>) option, and
bos@553 388 less when passed <option role="hg-opt-global">-q</option> (or
bos@553 389 <option role="hg-opt-global">--quiet</option>).</para>
bos@553 390
bos@553 391 </sect1>
bos@553 392 <sect1>
bos@553 393 <title>Making and reviewing changes</title>
bos@553 394
bos@553 395 <para>Now that we have a grasp of viewing history in Mercurial,
bos@553 396 let's take a look at making some changes and examining
bos@553 397 them.</para>
bos@553 398
bos@553 399 <para>The first thing we'll do is isolate our experiment in a
bos@553 400 repository of its own. We use the <command role="hg-cmd">hg
bos@553 401 clone</command> command, but we don't need to clone a copy of
bos@553 402 the remote repository. Since we already have a copy of it
bos@553 403 locally, we can just clone that instead. This is much faster
bos@553 404 than cloning over the network, and cloning a local repository
bos@553 405 uses less disk space in most cases, too. <!--
bos@553 406 &interaction.tour.reclone; --> As an aside, it's often good
bos@553 407 practice to keep a <quote>pristine</quote> copy of a remote
bos@553 408 repository around, which you can then make temporary clones of
bos@553 409 to create sandboxes for each task you want to work on. This
bos@553 410 lets you work on multiple tasks in parallel, each isolated from
bos@553 411 the others until it's complete and you're ready to integrate it
bos@553 412 back. Because local clones are so cheap, there's almost no
bos@553 413 overhead to cloning and destroying repositories whenever you
bos@553 414 want.</para>
bos@553 415
bos@553 416 <para>In our <filename class="directory">my-hello</filename>
bos@553 417 repository, we have a file <filename>hello.c</filename> that
bos@553 418 contains the classic <quote>hello, world</quote> program. Let's
bos@553 419 use the ancient and venerable <command>sed</command> command to
bos@553 420 edit this file so that it prints a second line of output. (I'm
bos@553 421 only using <command>sed</command> to do this because it's easy
bos@553 422 to write a scripted example this way. Since you're not under
bos@553 423 the same constraint, you probably won't want to use
bos@553 424 <command>sed</command>; simply use your preferred text editor to
bos@553 425 do the same thing.) <!-- &interaction.tour.sed; --></para>
bos@553 426
bos@553 427 <para>Mercurial's <command role="hg-cmd">hg status</command>
bos@553 428 command will tell us what Mercurial knows about the files in the
bos@553 429 repository. <!-- &interaction.tour.status; --> The <command
bos@553 430 role="hg-cmd">hg status</command> command prints no output for
bos@553 431 some files, but a line starting with
bos@553 432 <quote><literal>M</literal></quote> for
bos@553 433 <filename>hello.c</filename>. Unless you tell it to, <command
bos@553 434 role="hg-cmd">hg status</command> will not print any output
bos@553 435 for files that have not been modified.</para>
bos@553 436
bos@553 437 <para>The <quote><literal>M</literal></quote> indicates that
bos@553 438 Mercurial has noticed that we modified
bos@553 439 <filename>hello.c</filename>. We didn't need to
bos@553 440 <emphasis>inform</emphasis> Mercurial that we were going to
bos@553 441 modify the file before we started, or that we had modified the
bos@553 442 file after we were done; it was able to figure this out
bos@553 443 itself.</para>
bos@553 444
bos@553 445 <para>It's a little bit helpful to know that we've modified
bos@553 446 <filename>hello.c</filename>, but we might prefer to know
bos@553 447 exactly <emphasis>what</emphasis> changes we've made to it. To
bos@553 448 do this, we use the <command role="hg-cmd">hg diff</command>
bos@553 449 command. <!-- &interaction.tour.diff; --></para>
bos@553 450
bos@553 451 </sect1>
bos@553 452 <sect1>
bos@553 453 <title>Recording changes in a new changeset</title>
bos@553 454
bos@553 455 <para>We can modify files, build and test our changes, and use
bos@553 456 <command role="hg-cmd">hg status</command> and <command
bos@553 457 role="hg-cmd">hg diff</command> to review our changes, until
bos@553 458 we're satisfied with what we've done and arrive at a natural
bos@553 459 stopping point where we want to record our work in a new
bos@553 460 changeset.</para>
bos@553 461
bos@553 462 <para>The <command role="hg-cmd">hg commit</command> command lets
bos@553 463 us create a new changeset; we'll usually refer to this as
bos@553 464 <quote>making a commit</quote> or
bos@553 465 <quote>committing</quote>.</para>
bos@553 466
bos@553 467 <sect2>
bos@553 468 <title>Setting up a username</title>
bos@553 469
bos@553 470 <para>When you try to run <command role="hg-cmd">hg
bos@553 471 commit</command> for the first time, it is not guaranteed to
bos@553 472 succeed. Mercurial records your name and address with each
bos@553 473 change that you commit, so that you and others will later be
bos@553 474 able to tell who made each change. Mercurial tries to
bos@553 475 automatically figure out a sensible username to commit the
bos@553 476 change with. It will attempt each of the following methods,
bos@553 477 in order:</para>
bos@553 478 <orderedlist>
bos@553 479 <listitem><para>If you specify a <option
bos@553 480 role="hg-opt-commit">-u</option> option to the <command
bos@553 481 role="hg-cmd">hg commit</command> command on the command
bos@553 482 line, followed by a username, this is always given the
bos@553 483 highest precedence.</para></listitem>
bos@553 484 <listitem><para>If you have set the <envar>HGUSER</envar>
bos@553 485 environment variable, this is checked
bos@553 486 next.</para></listitem>
bos@553 487 <listitem><para>If you create a file in your home directory
bos@553 488 called <filename role="special">.hgrc</filename>, with a
bos@553 489 <envar role="rc-item-ui">username</envar> entry, that will
bos@553 490 be used next. To see what the contents of this file
bos@553 491 should look like, refer to section <xref
bos@553 492 id="sec:tour-basic:username"/>
bos@553 493 below.</para></listitem>
bos@553 494 <listitem><para>If you have set the <envar>EMAIL</envar>
bos@553 495 environment variable, this will be used
bos@553 496 next.</para></listitem>
bos@553 497 <listitem><para>Mercurial will query your system to find out
bos@553 498 your local user name and host name, and construct a
bos@553 499 username from these components. Since this often results
bos@553 500 in a username that is not very useful, it will print a
bos@553 501 warning if it has to do
bos@553 502 this.</para></listitem></orderedlist>
bos@553 503 <listitem><para>If all of these mechanisms fail, Mercurial will
bos@553 504 fail, printing an error message. In this case, it will not
bos@553 505 let you commit until you set up a
bos@553 506 username.</para></listitem>
bos@553 507 <listitem><para>You should think of the <envar>HGUSER</envar>
bos@553 508 environment variable and the <option
bos@553 509 role="hg-opt-commit">-u</option> option to the <command
bos@553 510 role="hg-cmd">hg commit</command> command as ways to
bos@553 511 <emphasis>override</emphasis> Mercurial's default selection
bos@553 512 of username. For normal use, the simplest and most robust
bos@553 513 way to set a username for yourself is by creating a
bos@553 514 <filename role="special">.hgrc</filename> file; see below
bos@553 515 for details.</para></listitem>
bos@553 516 <sect3>
bos@553 517 <title>Creating a Mercurial configuration file</title>
bos@553 518 <listitem><para>\label{sec:tour-basic:username}</para></listitem>
bos@553 519 <listitem><para>To set a user name, use your favourite editor
bos@553 520 to create a file called <filename
bos@553 521 role="special">.hgrc</filename> in your home directory.
bos@553 522 Mercurial will use this file to look up your personalised
bos@553 523 configuration settings. The initial contents of your
bos@553 524 <filename role="special">.hgrc</filename> should look like
bos@553 525 this.</para></listitem><programlisting>
bos@553 526 <listitem><para> # This is a Mercurial configuration file.
bos@553 527 [ui] username = Firstname Lastname
bos@553 528 &lt;email.address@domain.net&gt;</para></listitem></programlisting>
bos@553 529 <listitem><para>The <quote><literal>[ui]</literal></quote>
bos@553 530 line begins a <emphasis>section</emphasis> of the config
bos@553 531 file, so you can read the <quote><literal>username =
bos@553 532 ...</literal></quote> line as meaning <quote>set the
bos@553 533 value of the <literal>username</literal> item in the
bos@553 534 <literal>ui</literal> section</quote>. A section
bos@553 535 continues until a new section begins, or the end of the
bos@553 536 file. Mercurial ignores empty lines and treats any text
bos@553 537 from <quote><literal>#</literal></quote> to the end of a
bos@553 538 line as a comment.</para></listitem>
bos@553 539 </sect3>
bos@553 540 <sect3>
bos@553 541 <title>Choosing a user name</title>
bos@553 542
bos@553 543 <listitem><para>You can use any text you like as the value of
bos@553 544 the <literal>username</literal> config item, since this
bos@553 545 information is for reading by other people, but for
bos@553 546 interpreting by Mercurial. The convention that most
bos@553 547 people follow is to use their name and email address, as
bos@553 548 in the example above.</para></listitem>
bos@553 549 <note>
bos@553 550 <listitem><para> Mercurial's built-in web server obfuscates
bos@553 551 email addresses, to make it more difficult for the email
bos@553 552 harvesting tools that spammers use. This reduces the
bos@553 553 likelihood that you'll start receiving more junk email
bos@553 554 if you publish a Mercurial repository on the
bos@553 555 web.</para></listitem></note>
bos@553 556
bos@553 557 </sect3>
bos@553 558 </sect2>
bos@553 559 <sect2>
bos@553 560 <title>Writing a commit message</title>
bos@553 561
bos@553 562 <listitem><para>When we commit a change, Mercurial drops us into
bos@553 563 a text editor, to enter a message that will describe the
bos@553 564 modifications we've made in this changeset. This is called
bos@553 565 the <emphasis>commit message</emphasis>. It will be a
bos@553 566 record for readers of what we did and why, and it will be
bos@553 567 printed by <command role="hg-cmd">hg log</command> after
bos@553 568 we've finished committing. <!-- &interaction.tour.commit;
bos@553 569 --></para></listitem>
bos@553 570 <listitem><para>The editor that the <command role="hg-cmd">hg
bos@553 571 commit</command> command drops us into will contain an
bos@553 572 empty line, followed by a number of lines starting with
bos@553 573 <quote><literal>HG:</literal></quote>.</para></listitem><programlisting>
bos@553 574 <listitem><para> <emphasis>empty line</emphasis> HG: changed
bos@553 575 hello.c</para></listitem></programlisting>
bos@553 576 <listitem><para>Mercurial ignores the lines that start with
bos@553 577 <quote><literal>HG:</literal></quote>; it uses them only to
bos@553 578 tell us which files it's recording changes to. Modifying or
bos@553 579 deleting these lines has no effect.</para></listitem>
bos@553 580 </sect2>
bos@553 581 <sect2>
bos@553 582 <title>Writing a good commit message</title>
bos@553 583
bos@553 584 <listitem><para>Since <command role="hg-cmd">hg log</command>
bos@553 585 only prints the first line of a commit message by default,
bos@553 586 it's best to write a commit message whose first line stands
bos@553 587 alone. Here's a real example of a commit message that
bos@553 588 <emphasis>doesn't</emphasis> follow this guideline, and
bos@553 589 hence has a summary that is not
bos@553 590 readable.</para></listitem><programlisting>
bos@553 591 <listitem><para> changeset: 73:584af0e231be user: Censored
bos@553 592 Person &lt;censored.person@example.org&gt; date: Tue Sep
bos@553 593 26 21:37:07 2006 -0700 summary: include
bos@553 594 buildmeister/commondefs. Add an exports and
bos@553 595 install</para></listitem></programlisting>
bos@553 596
bos@553 597 <listitem><para>As far as the remainder of the contents of the
bos@553 598 commit message are concerned, there are no hard-and-fast
bos@553 599 rules. Mercurial itself doesn't interpret or care about the
bos@553 600 contents of the commit message, though your project may have
bos@553 601 policies that dictate a certain kind of
bos@553 602 formatting.</para></listitem>
bos@553 603 <listitem><para>My personal preference is for short, but
bos@553 604 informative, commit messages that tell me something that I
bos@553 605 can't figure out with a quick glance at the output of
bos@553 606 <command role="hg-cmd">hg log
bos@553 607 --patch</command>.</para></listitem>
bos@553 608 </sect2>
bos@553 609 <sect2>
bos@553 610 <title>Aborting a commit</title>
bos@553 611
bos@553 612 <listitem><para>If you decide that you don't want to commit
bos@553 613 while in the middle of editing a commit message, simply exit
bos@553 614 from your editor without saving the file that it's editing.
bos@553 615 This will cause nothing to happen to either the repository
bos@553 616 or the working directory.</para></listitem>
bos@553 617 <listitem><para>If we run the <command role="hg-cmd">hg
bos@553 618 commit</command> command without any arguments, it records
bos@553 619 all of the changes we've made, as reported by <command
bos@553 620 role="hg-cmd">hg status</command> and <command
bos@553 621 role="hg-cmd">hg diff</command>.</para></listitem>
bos@553 622 </sect2>
bos@553 623 <sect2>
bos@553 624 <title>Admiring our new handiwork</title>
bos@553 625
bos@553 626 <listitem><para>Once we've finished the commit, we can use the
bos@553 627 <command role="hg-cmd">hg tip</command> command to display
bos@553 628 the changeset we just created. This command produces output
bos@553 629 that is identical to <command role="hg-cmd">hg
bos@553 630 log</command>, but it only displays the newest revision in
bos@553 631 the repository. <!-- &interaction.tour.tip; --> We refer to
bos@553 632 the newest revision in the repository as the tip revision,
bos@553 633 or simply the tip.</para></listitem>
bos@553 634 </sect2>
bos@553 635 </sect1>
bos@553 636 <sect1>
bos@553 637 <title>Sharing changes</title>
bos@553 638
bos@553 639 <listitem><para>We mentioned earlier that repositories in
bos@553 640 Mercurial are self-contained. This means that the changeset
bos@553 641 we just created exists only in our <filename
bos@553 642 class="directory">my-hello</filename> repository. Let's
bos@553 643 look at a few ways that we can propagate this change into
bos@553 644 other repositories.</para></listitem>
bos@553 645 <sect2>
bos@553 646 <title>Pulling changes from another repository</title>
bos@553 647 <listitem><para>\label{sec:tour:pull}</para></listitem>
bos@553 648 <listitem><para>To get started, let's clone our original
bos@553 649 <filename class="directory">hello</filename> repository,
bos@553 650 which does not contain the change we just committed. We'll
bos@553 651 call our temporary repository <filename
bos@553 652 class="directory">hello-pull</filename>. <!--
bos@553 653 &interaction.tour.clone-pull; --></para></listitem>
bos@553 654 <listitem><para>We'll use the <command role="hg-cmd">hg
bos@553 655 pull</command> command to bring changes from <filename
bos@553 656 class="directory">my-hello</filename> into <filename
bos@553 657 class="directory">hello-pull</filename>. However, blindly
bos@553 658 pulling unknown changes into a repository is a somewhat
bos@553 659 scary prospect. Mercurial provides the <command
bos@553 660 role="hg-cmd">hg incoming</command> command to tell us
bos@553 661 what changes the <command role="hg-cmd">hg pull</command>
bos@553 662 command <emphasis>would</emphasis> pull into the repository,
bos@553 663 without actually pulling the changes in. <!--
bos@553 664 &interaction.tour.incoming; --> (Of course, someone could
bos@553 665 cause more changesets to appear in the repository that we
bos@553 666 ran <command role="hg-cmd">hg incoming</command> in, before
bos@553 667 we get a chance to <command role="hg-cmd">hg pull</command>
bos@553 668 the changes, so that we could end up pulling changes that we
bos@553 669 didn't expect.)</para></listitem>
bos@553 670 <listitem><para>Bringing changes into a repository is a simple
bos@553 671 matter of running the <command role="hg-cmd">hg
bos@553 672 pull</command> command, and telling it which repository to
bos@553 673 pull from. <!-- &interaction.tour.pull; --> As you can see
bos@553 674 from the before-and-after output of <command
bos@553 675 role="hg-cmd">hg tip</command>, we have successfully
bos@553 676 pulled changes into our repository. There remains one step
bos@553 677 before we can see these changes in the working
bos@553 678 directory.</para></listitem>
bos@553 679 </sect2>
bos@553 680 <sect2>
bos@553 681 <title>Updating the working directory</title>
bos@553 682
bos@553 683 <listitem><para>We have so far glossed over the relationship
bos@553 684 between a repository and its working directory. The
bos@553 685 <command role="hg-cmd">hg pull</command> command that we ran
bos@553 686 in section <xref id="sec:tour:pull"/> brought changes into
bos@553 687 the
bos@553 688 repository, but if we check, there's no sign of those
bos@553 689 changes in the working directory. This is because <command
bos@553 690 role="hg-cmd">hg pull</command> does not (by default)
bos@553 691 touch the working directory. Instead, we use the <command
bos@553 692 role="hg-cmd">hg update</command> command to do this. <!--
bos@553 693 &interaction.tour.update; --></para></listitem>
bos@553 694 <listitem><para>It might seem a bit strange that <command
bos@553 695 role="hg-cmd">hg pull</command> doesn't update the working
bos@553 696 directory automatically. There's actually a good reason for
bos@553 697 this: you can use <command role="hg-cmd">hg update</command>
bos@553 698 to update the working directory to the state it was in at
bos@553 699 <emphasis>any revision</emphasis> in the history of the
bos@553 700 repository. If you had the working directory updated to an
bos@553 701 old revision---to hunt down the origin of a bug, say---and
bos@553 702 ran a <command role="hg-cmd">hg pull</command> which
bos@553 703 automatically updated the working directory to a new
bos@553 704 revision, you might not be terribly happy.</para></listitem>
bos@553 705 <listitem><para>However, since pull-then-update is such a common
bos@553 706 thing to do, Mercurial lets you combine the two by passing
bos@553 707 the <option role="hg-opt-pull">-u</option> option to
bos@553 708 <command role="hg-cmd">hg
bos@553 709 pull</command>.</para></listitem><programlisting>
bos@553 710 <listitem><para> hg pull
bos@553 711 -u</para></listitem></programlisting>
bos@553 712 <listitem><para>If you look back at the output of <command
bos@553 713 role="hg-cmd">hg pull</command> in section <xref
bos@553 714 id="sec:tour:pull"/> when we ran it without <option
bos@553 715 role="hg-opt-pull">-u</option>, you can see that it
bos@553 716 printed a helpful reminder that we'd have to take an
bos@553 717 explicit step to update the working
bos@553 718 directory:</para></listitem><programlisting>
bos@553 719 <listitem><para> (run 'hg update' to get a working
bos@553 720 copy)</para></listitem></programlisting>
bos@553 721
bos@553 722 <listitem><para>To find out what revision the working directory
bos@553 723 is at, use the <command role="hg-cmd">hg parents</command>
bos@553 724 command. <!-- &interaction.tour.parents; --> If you look
bos@553 725 back at figure <xref id="fig:tour-basic:history"/>, you'll
bos@553 726 see arrows connecting each changeset. The node that the
bos@553 727 arrow leads <emphasis>from</emphasis> in each case is a
bos@553 728 parent, and the node that the arrow leads
bos@553 729 <emphasis>to</emphasis> is its child. The working directory
bos@553 730 has a parent in just the same way; this is the changeset
bos@553 731 that the working directory currently
bos@553 732 contains.</para></listitem>
bos@553 733 <listitem><para>To update the working directory to a particular
bos@553 734 revision, give a revision number or changeset ID to the
bos@553 735 <command role="hg-cmd">hg update</command> command. <!--
bos@553 736 &interaction.tour.older; --> If you omit an explicit
bos@553 737 revision, <command role="hg-cmd">hg update</command> will
bos@553 738 update to the tip revision, as shown by the second call to
bos@553 739 <command role="hg-cmd">hg update</command> in the example
bos@553 740 above.</para></listitem>
bos@553 741 </sect2>
bos@553 742 <sect2>
bos@553 743 <title>Pushing changes to another repository</title>
bos@553 744
bos@553 745 <listitem><para>Mercurial lets us push changes to another
bos@553 746 repository, from the repository we're currently visiting.
bos@553 747 As with the example of <command role="hg-cmd">hg
bos@553 748 pull</command> above, we'll create a temporary repository
bos@553 749 to push our changes into. <!-- &interaction.tour.clone-push;
bos@553 750 --> The <command role="hg-cmd">hg outgoing</command> command
bos@553 751 tells us what changes would be pushed into another
bos@553 752 repository. <!-- &interaction.tour.outgoing; --> And the
bos@553 753 <command role="hg-cmd">hg push</command> command does the
bos@553 754 actual push. <!-- &interaction.tour.push; --> As with
bos@553 755 <command role="hg-cmd">hg pull</command>, the <command
bos@553 756 role="hg-cmd">hg push</command> command does not update
bos@553 757 the working directory in the repository that it's pushing
bos@553 758 changes into. (Unlike <command role="hg-cmd">hg
bos@553 759 pull</command>, <command role="hg-cmd">hg push</command>
bos@553 760 does not provide a <literal>-u</literal> option that updates
bos@553 761 the other repository's working directory.)</para></listitem>
bos@553 762 <listitem><para>What happens if we try to pull or push changes
bos@553 763 and the receiving repository already has those changes?
bos@553 764 Nothing too exciting. <!-- &interaction.tour.push.nothing;
bos@553 765 --></para></listitem>
bos@553 766 </sect2>
bos@553 767 <sect2>
bos@553 768 <title>Sharing changes over a network</title>
bos@553 769
bos@553 770 <listitem><para>The commands we have covered in the previous few
bos@553 771 sections are not limited to working with local repositories.
bos@553 772 Each works in exactly the same fashion over a network
bos@553 773 connection; simply pass in a URL instead of a local path.
bos@553 774 <!-- &interaction.tour.outgoing.net; --> In this example, we
bos@553 775 can see what changes we could push to the remote repository,
bos@553 776 but the repository is understandably not set up to let
bos@553 777 anonymous users push to it. <!-- &interaction.tour.push.net;
bos@553 778 --></para></listitem>
bos@553 779 </sect2>
bos@553 780 </sect1>
bos@553 781 </chapter>
bos@553 782
bos@553 783 <!--
bos@553 784 local variables:
bos@553 785 sgml-parent-document: ("00book.xml" "book" "chapter")
bos@553 786 end:
bos@553 787 -->