hgbook

view en/ch02-tour-basic.xml @ 621:1ef7708b3b7f

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