hgbook

annotate en/ch01-tour-basic.xml @ 595:e0a4ba81f888

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