hgbook

view en/tour-basic.tex @ 97:659fa1a2c628

More text.
author Bryan O'Sullivan <bos@serpentine.com>
date Fri Oct 13 15:48:44 2006 -0700 (2006-10-13)
parents 7d7ddc3a57af
children 06383f9e46e4
line source
1 \chapter{A tour of Mercurial: the basics}
2 \label{chap:tour-basic}
4 \section{Installing Mercurial on your system}
5 \label{sec:tour:install}
7 Prebuilt binary packages of Mercurial are available for every popular
8 operating system. These make it easy to start using Mercurial on your
9 computer immediately.
11 \subsection{Linux}
13 Because each Linux distribution has its own packaging tools, policies,
14 and rate of development, it's difficult to give a comprehensive set of
15 instructions on how to install Mercurial binaries. The version of
16 Mercurial that you will end up with can vary depending on how active
17 the person is who maintains the package for your distribution.
19 To keep things simple, I will focus on installing Mercurial from the
20 command line under the most popular Linux distributions. Most of
21 these distributions provide graphical package managers that will let
22 you install Mercurial with a single click; the package name to look
23 for is \texttt{mercurial}.
25 \begin{itemize}
26 \item[Debian]
27 \begin{codesample4}
28 apt-get install mercurial
29 \end{codesample4}
31 \item[Fedora Core]
32 \begin{codesample4}
33 yum install mercurial
34 \end{codesample4}
36 \item[Gentoo]
37 \begin{codesample4}
38 emerge mercurial
39 \end{codesample4}
41 \item[OpenSUSE]
42 \begin{codesample4}
43 yum install mercurial
44 \end{codesample4}
46 \item[Ubuntu] Ubuntu's Mercurial package is particularly old, and you
47 should not use it. If you know how, you can rebuild and install the
48 Debian package. It's probably easier to build Mercurial from source
49 and simply run that; see section~\ref{sec:srcinstall:unixlike} for
50 details.
51 \end{itemize}
53 \subsection{Mac OS X}
55 Lee Cantey publishes an installer of Mercurial for Mac OS~X at
56 \url{http://mercurial.berkwood.com}. This package works on both
57 Intel-~and Power-based Macs. Before you can use it, you must install
58 a compatible version of Universal MacPython~\cite{web:macpython}. This
59 is easy to do; simply follow the instructions on Lee's site.
61 \subsection{Solaris}
63 XXX.
65 \subsection{Windows}
67 Lee Cantey publishes an installer of Mercurial for Windows at
68 \url{http://mercurial.berkwood.com}. This package has no external
69 dependencies; it ``just works''.
71 \begin{note}
72 The Windows version of Mercurial does not automatically convert line
73 endings between Windows and Unix styles. If you want to share work
74 with Unix users, you must do a little additional configuration
75 work. XXX Flesh this out.
76 \end{note}
78 \section{Getting started}
80 To begin, we'll use the \hgcmd{version} command to find out whether
81 Mercurial is actually installed properly. The actual version
82 information that it prints isn't so important; it's whether it prints
83 anything at all that we care about.
84 \interaction{tour.version}
86 \subsection{Built-in help}
88 Mercurial provides a built-in help system. This invaluable for those
89 times when you find yourself stuck trying to remember how to run a
90 command. If you are completely stuck, simply run \hgcmd{help}; it
91 will print a brief list of commands, along with a description of what
92 each does. If you ask for help on a specific command (as below), it
93 prints more detailed information.
94 \interaction{tour.help}
95 For a more impressive level of detail (which you won't usually need)
96 run \hgcmdargs{help}{\hggopt{-v}}. The \hggopt{-v} option is short
97 for \hggopt{--verbose}, and tells Mercurial to print more information
98 than it usually would.
100 \section{Working with a repository}
102 In Mercurial, everything happens inside a \emph{repository}. The
103 repository for a project contains all of the files that ``belong to''
104 that project, along with a historical record of the project's files.
106 There's nothing particularly magical about a repository; it is simply
107 a directory tree in your filesystem that Mercurial treats as special.
108 You can rename delete a repository any time you like, using either the
109 command line or your file browser.
111 \subsection{Making a local copy of a repository}
113 \emph{Copying} a repository is just a little bit special. While you
114 could use a normal file copying command to make a copy of a
115 repository, it's best to use a built-in command that Mercurial
116 provides. This command is called \hgcmd{clone}, because it creates an
117 identical copy of an existing repository.
118 \interaction{tour.clone}
119 If our clone succeeded, we should now have a local directory called
120 \dirname{hello}. This directory will contain some files.
121 \interaction{tour.ls}
122 These files have the same contents and history in our repository as
123 they do in the repository we cloned.
125 Every Mercurial repository is complete, self-contained, and
126 independent. It contains its own private copy of a project's files
127 and history. A cloned repository remembers the location of the
128 repository it was cloned from, but it does not communicate with that
129 repository, or any other, unless you tell it to.
131 What this means for now is that we're free to experiment with our
132 repository, safe in the knowledge that it's a private ``sandbox'' that
133 won't affect anyone else.
135 \subsection{What's in a repository?}
137 When we take a more detailed look inside a repository, we can see that
138 it contains a directory named \dirname{.hg}. This is where Mercurial
139 keeps all of its metadata for the repository.
140 \interaction{tour.ls-a}
142 The contents of the \dirname{.hg} directory and its subdirectories are
143 private to Mercurial. Every other file and directory in the
144 repository is yours to do with as you please.
146 To introduce a little terminology, the \dirname{.hg} directory is the
147 ``real'' repository, and all of the files and directories that coexist
148 with it are said to live in the \emph{working directory}. An easy way
149 to remember the distinction is that the \emph{repository} contains the
150 \emph{history} of your project, while the \emph{working directory}
151 contains a \emph{snapshot} of your project at a particular point in
152 history.
154 \section{A tour through history}
156 One of the first things we might want to do with a new, unfamiliar
157 repository is understand its history. The \hgcmd{log} command gives
158 us a view of history.
159 \interaction{tour.log}
160 By default, this command prints a brief paragraph of output for each
161 change to the project that was recorded. In Mercurial terminology, we
162 call each of these recorded events a \emph{changeset}, because it can
163 contain a record of changes to several files.
165 The fields in a record of output from \hgcmd{log} are as follows.
166 \begin{itemize}
167 \item[\texttt{changeset}] This field has the format of a number,
168 followed by a colon, followed by a hexadecimal string. These are
169 \emph{identifiers} for the changeset. There are two identifiers
170 because the number is shorter and easier to type than the hex
171 string.
172 \item[\texttt{user}] The identity of the person who created the
173 changeset. This is a free-form field, but it most often contains a
174 person's name and email address.
175 \item[\texttt{date}] The date and time on which the changeset was
176 created, and the timezone in which it was created. (Thef date and
177 time are local to that timezone; they display what time and date it
178 was for the person who created the changeset.)
179 \item[\texttt{summary}] The first line of the text message that the
180 creator of the changeset entered to describe the changeset.
181 \end{itemize}
182 The default output printed by \hgcmd{log} is purely a summary; it is
183 missing a lot of detail.
185 Figure~\ref{fig:tour:history} provides a graphical representation of
186 the history of the \dirname{hello} repository, to make it a little
187 easier to see which direction history is ``flowing'' in. We'll be
188 returning to this figure several times in this chapter and the chapter
189 that follows.
191 \begin{figure}[ht]
192 \centering
193 \grafix{tour-history}
194 \caption{Graphical history of the \dirname{hello} repository}
195 \label{fig:tour:history}
196 \end{figure}
198 \subsection{Changesets, revisions, and talking to other
199 people}
201 As English is a notoriously sloppy language, and computer science has
202 a history of terminological confusion, revision control has a variety
203 of terms that have the same meaning. If you are talking about
204 Mercurial history with other people, you will find that the word
205 ``changeset'' is often compressed to ``change'' or (when written)
206 ``cset'', and sometimes a changeset is referred to as a ``revision''
207 or a ``rev''.
209 While it doesn't matter what \emph{word} you use to refer to the
210 concept of ``a~changeset'', the \emph{identifier} that you use to
211 refer to ``a~\emph{specific} changeset'' is of great importance.
212 Recall that the \texttt{changeset} field in the output from
213 \hgcmd{log} identifies a changeset using both a number and a
214 hexadecimal string.
215 \begin{itemize}
216 \item The revision number is \emph{only valid in that repository},
217 \item while the hex string is the \emph{permanent, unchanging
218 identifier} that will always identify that exact changeset in
219 \emph{every} copy of the repository.
220 \end{itemize}
221 This distinction is important. If you send someone an email talking
222 about ``revision~33'', there's a high likelihood that their
223 revision~33 will \emph{not be the same} as yours. The reason for this
224 is that a revision number depends on the order in which changes
225 arrived in a repository, and there is no guarantee that the same
226 changes will happen in the same order in different repositories.
227 Three changes $a,b,c$ can easily appear in one repository as $0,1,2$,
228 while in another as $1,0,2$.
230 Mercurial uses revision numbers purely as a convenient shorthand. If
231 you need to discuss a changeset with someone, or make a record of a
232 changeset for some other reason (for example, in a bug report), use
233 the hexadecimal identifier.
235 \subsection{Viewing specific revisions}
237 To narrow the output of \hgcmd{log} down to a single revision, use the
238 \hgopt{log}{-r} (or \hgopt{log}{--rev}) option. You can use either a
239 revision number or a long-form changeset identifier, and you can
240 provide as many revisions as you want. \interaction{tour.log-r}
242 If you want to see the history of several revisions without having to
243 list each one, you can use \emph{range notation}; this lets you
244 express the idea ``I want all revisions between $a$ and $b$,
245 inclusive''.
246 \interaction{tour.log.range}
247 Mercurial also honours the order in which you specify revisions, so
248 \hgcmdargs{log}{-r 2:4} prints $2,3,4$ while \hgcmdargs{log}{-r 4:2}
249 prints $4,3,2$.
251 \subsection{More detailed information}
253 While the summary information printed by \hgcmd{log} is useful if you
254 already know what you're looking for, you may need to see a complete
255 description of the change, or a list of the files changed, if you're
256 trying to decide whether a changeset is the one you're looking for.
257 The \hgcmd{log} command's \hggopt{-v} (or \hggopt{--verbose})
258 option gives you this extra detail.
259 \interaction{tour.log-v}
261 If you want to see both the description and content of a change, add
262 the \hgopt{log}{-p} (or \hgopt{log}{--patch}) option. This displays
263 the content of a change as a \emph{unified diff} (if you've never seen
264 a unified diff before, see section~\ref{sec:mq:patch} for an overview).
265 \interaction{tour.log-vp}
267 \section{All about command options}
269 Let's take a brief break from exploring Mercurial commands to discuss
270 a pattern in the way that they work; you may find this useful to keep
271 in mind as we continiue our tour.
273 Mercurial has a consistent and straightforward approach to dealing
274 with the options that you can pass to commands. It follows the
275 conventions for options that are common to modern Linux and Unix
276 systems.
277 \begin{itemize}
278 \item Every option has a long name. For example, as we've already
279 seen, the \hgcmd{log} command accepts a \hgopt{log}{--rev} option.
280 \item Most options have short names, too. Instead of
281 \hgopt{log}{--rev}, we can use \hgopt{log}{-r}. (The reason that
282 some options don't have short names is that the options in question
283 are rarely used.)
284 \item Long options start with two dashes (e.g.~\hgopt{log}{--rev}),
285 while short options start with one (e.g.~\hgopt{log}{-r}).
286 \item Option naming and usage is consistent across commands. For
287 example, every command that lets you specify a changeset~ID or
288 revision number accepts both \hgopt{log}{-r} and \hgopt{log}{--rev}
289 arguments.
290 \end{itemize}
291 In the examples throughout this book, I use short options instead of
292 long. This just reflects my own preference, so don't read anything
293 significant into it.
295 Most commands that print output of some kind will print more output
296 when passed a \hggopt{-v} (or \hggopt{--verbose}) option, and less
297 when passed \hggopt{-q} (or \hggopt{--quiet}).
299 \section{Making and reviewing changes}
301 Now that we have a grasp of viewing history in Mercurial, let's take a
302 look at making some changes and examining them.
304 The first thing we'll do is isolate our experiment in a repository of
305 its own. We use the \hgcmd{clone} command, but we don't need to
306 clone a copy of the remote repository. Since we already have a copy
307 of it locally, we can just clone that instead. This is much faster
308 than cloning over the network, and cloning a local repository uses
309 less disk space in most cases, too.
310 \interaction{tour.reclone}
311 As an aside, it's often good practice to keep a ``pristine'' copy of a
312 remote repository around, which you can then make temporary clones of
313 to create sandboxes for each task you want to work on. This lets you
314 work on multiple tasks in parallel, each isolated from the others
315 until it's complete and you're ready to integrate it back. Because
316 local clones are so cheap, there's almost no overhead to cloning and
317 destroying repositories whenever you want.
319 In our \dirname{my-hello} repository, we have a file
320 \filename{hello.c} that contains the classic ``hello, world'' program.
321 Let's use the ancient and venerable \command{sed} command to edit this
322 file so that it prints a second line of output. (I'm only using
323 \command{sed} to do this because it's easy to write a scripted example
324 this way. Since you're not under the same constraint, you probably
325 won't want to use \command{sed}; simply use your preferred text editor to
326 do the same thing.)
327 \interaction{tour.sed}
329 Mercurial's \hgcmd{status} command will tell us what Mercurial knows
330 about the files in the repository.
331 \interaction{tour.status}
332 The \hgcmd{status} command prints no output for some files, but a line
333 starting with ``\texttt{M}'' for \filename{hello.c}. Unless you tell
334 it to, \hgcmd{status} will not print any output for files that have
335 not been modified.
337 The ``\texttt{M}'' indicates that Mercurial has noticed that we
338 modified \filename{hello.c}. We didn't need to \emph{inform}
339 Mercurial that we were going to modify the file before we started, or
340 that we had modified the file after we were done; it was able to
341 figure this out itself.
343 It's a little bit helpful to know that we've modified
344 \filename{hello.c}, but we might prefer to know exactly \emph{what}
345 changes we've made to it. To do this, we use the \hgcmd{diff}
346 command.
347 \interaction{tour.diff}
349 \section{Recording changes in a new changeset}
351 We can modify files, build and test our changes, and use
352 \hgcmd{status} and \hgcmd{diff} to review our changes, until we're
353 satisfied with what we've done and arrive at a natural stopping point
354 where we want to record our work in a new changeset.
356 The \hgcmd{commit} command lets us create a new changeset; we'll
357 usually refer to this as ``making a commit'' or ``committing''.
359 \subsection{Writing a commit message}
361 When we commit a change, Mercurial drops us into a text editor, to
362 enter a message that will describe the modifications we've made in
363 this changeset. This is called the \emph{commit message}. It will be
364 a record for readers of what we did and why, and it will be printed by
365 \hgcmd{log} after we've finished committing.
366 \interaction{tour.commit}
368 The editor that the \hgcmd{commit} command drops us into will contain
369 an empty line, followed by a number of lines starting with
370 ``\texttt{HG:}''.
371 \begin{codesample2}
372 \emph{empty line}
373 HG: changed hello.c
374 \end{codesample2}
375 Mercurial ignores the lines that start with ``\texttt{HG:}''; it uses
376 them only to tell us which files it's recording changes to. Modifying
377 or deleting these lines has no effect.
379 \subsection{Writing a good commit message}
381 Since \hgcmd{log} only prints the first line of a commit message by
382 default, it's best to write a commit message whose first line stands
383 alone. Here's a real example of a commit message that \emph{doesn't}
384 follow this guideline, and hence has a summary that is not readable.
385 \begin{codesample2}
386 changeset: 73:584af0e231be
387 user: Censored Person <censored.person@example.org>
388 date: Tue Sep 26 21:37:07 2006 -0700
389 summary: include buildmeister/commondefs. Add an exports and install
390 \end{codesample2}
392 As far as the remainder of the contents of the commit message are
393 concerned, there are no hard-and-fast rules. Mercurial itself doesn't
394 interpret or care about the contents of the commit message, though
395 your project may have policies that dictate a certain kind of
396 formatting.
398 My personal preference is for short, but informative, commit messages
399 that tell me something that I can't figure out with a quick glance at
400 the output of \hgcmdargs{log}{--patch}.
402 \subsection{Aborting a commit}
404 If you decide that you don't want to commit while in the middle of
405 editing a commit message, simply exit from your editor without saving
406 the file that it's editing. This will cause nothing to happen to
407 either the repository or the working directory.
409 If we run the \hgcmd{commit} command without any arguments, it records
410 all of the changes we've made, as reported by \hgcmd{status} and
411 \hgcmd{diff}.
413 \subsection{Admiring our new handywork}
415 Once we've finished the commit, we can use the \hgcmd{tip} command to
416 display the changeset we just created. This command produces output
417 that is identical to \hgcmd{log}, but it only displays the newest
418 revision in the repository.
419 \interaction{tour.tip}
420 We refer to the newest revision in the repository as the tip revision,
421 or simply the tip.
423 \section{Sharing changes}
425 We mentioned earlier that repositories in Mercurial are
426 self-contained. This means that the changeset we just created exists
427 only in our \dirname{my-hello} repository. Let's look at a few ways
428 that we can propagate this change into other repositories.
430 \subsection{Pulling changes from another repository}
431 \label{sec:tour:pull}
433 To get started, let's clone our original \dirname{hello} repository,
434 which does not contain the change we just committed. We'll call our
435 temporary repository \dirname{hello-pull}.
436 \interaction{tour.clone-pull}
438 We'll use the \hgcmd{pull} command to bring changes from
439 \dirname{my-hello} into \dirname{hello-pull}. However, blindly
440 pulling unknown changes into a repository is a somewhat scary
441 prospect. Mercurial provides the \hgcmd{incoming} command to tell us
442 what changes the \hgcmd{pull} command \emph{would} pull into the
443 repository, without actually pulling the changes in.
444 \interaction{tour.incoming}
445 (Of course, someone could cause more changesets to appear in the
446 repository that we ran \hgcmd{incoming} in, before we get a chance to
447 \hgcmd{pull} the changes, so that we could end up pulling changes that we
448 didn't expect.)
450 Bringing changes into a repository is a simple matter of running the
451 \hgcmd{pull} command, and telling it which repository to pull from.
452 \interaction{tour.pull}
453 As you can see from the before-and-after output of \hgcmd{tip}, we
454 have successfully pulled changes into our repository. There remains
455 one step before we can see these changes in the working directory.
457 \subsection{Updating the working directory}
459 We have so far glossed over the relationship between a repository and
460 its working directory. The \hgcmd{pull} command that we ran in
461 section~\ref{sec:tour:pull} brought changes into the repository, but
462 if we check, there's no sign of those changes in the working
463 directory. This is because \hgcmd{pull} does not (by default) touch
464 the working directory. Instead, we use the \hgcmd{update} command to
465 do this.
466 \interaction{tour.update}
468 It might seem a bit strange that \hgcmd{pull} doesn't update the
469 working directory automatically. There's actually a good reason for
470 this: you can use \hgcmd{update} to update the working directory to
471 the state it was in at \emph{any revision} in the history of the
472 repository. If you had the working directory updated to an old
473 revision---to hunt down the origin of a bug, say---and ran a
474 \hgcmd{pull} which automatically updated the working directory to a
475 new revision, you might not be terribly happy.
477 However, since pull-then-update is such a common thing to do,
478 Mercurial lets you combine the two by passing the \hgopt{pull}{-u}
479 option to \hgcmd{pull}.
480 \begin{codesample2}
481 hg pull -u
482 \end{codesample2}
483 If you look back at the output of \hgcmd{pull} in
484 section~\ref{sec:tour:pull} when we ran it without \hgopt{pull}{-u},
485 you can see that it printed a helpful reminder that we'd have to take
486 an explicit step to update the working directory:
487 \begin{codesample2}
488 (run 'hg update' to get a working copy)
489 \end{codesample2}
491 To find out what revision the working directory is at, use the
492 \hgcmd{parents} command.
493 \interaction{tour.parents}
494 To update the working directory to a particular revision, give a
495 revision number or changeset~ID to the \hgcmd{update} command.
496 \interaction{tour.older}
497 If you omit an explicit revision, \hgcmd{update} will update to the
498 tip revision, as shown by the second call to \hgcmd{update} in the
499 example above.
501 \subsection{Pushing changes to another repository}
503 Mercurial lets us push changes to another repository, from the
504 repository we're currently visiting. As with the example of
505 \hgcmd{pull} above, we'll create a temporary repository to push our
506 changes into.
507 \interaction{tour.clone-push}
508 The \hgcmd{outgoing} command tells us what changes would be pushed
509 into another repository.
510 \interaction{tour.outgoing}
511 And the \hgcmd{push} command does the actual push.
512 \interaction{tour.push}
513 As with \hgcmd{pull}, the \hgcmd{push} command does not update the
514 working directory in the repository that it's pushing changes into.
515 (Unlike \hgcmd{pull}, \hgcmd{push} does not provide a \texttt{-u}
516 option that updates the other repository's working directory.)
518 What happens if we try to pull or push changes and the receiving
519 repository already has those changes? Nothing too exciting.
520 \interaction{tour.push.nothing}
522 \subsection{Sharing changes over a network}
524 The commands we have covered in the previous few sections are not
525 limited to working with local repositories. Each works in exactly the
526 same fashion over a network connection; simply pass in a URL instead
527 of a local path.
528 \interaction{tour.outgoing.net}
529 In this example, we can see what changes we could push to the remote
530 repository, but the repository is understandably not set up to let
531 anonymous users push to it.
532 \interaction{tour.push.net}
534 %%% Local Variables:
535 %%% mode: latex
536 %%% TeX-master: "00book"
537 %%% End: