hgbook

view en/ch02-tour-basic.xml @ 645:1114da00d30e

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