bos@553: bos@553: bos@559: bos@572: bos@553: A tour of Mercurial: the basics bos@559: bos@559: bos@553: Installing Mercurial on your system bos@553: bos@584: Prebuilt binary packages of Mercurial are available for bos@553: every popular operating system. These make it easy to start bos@553: using Mercurial on your computer immediately. bos@553: bos@553: bos@609: Windows bos@609: bos@609: The best version of Mercurial for Windows is bos@609: TortoiseHg, which can be found at http://bitbucket.org/tortoisehg/stable/wiki/Home. bos@609: This package has no external dependencies; it just bos@609: works. It provides both command line and graphical bos@609: user interfaces. bos@609: bos@609: bos@609: bos@609: bos@609: Mac OS X bos@609: bos@609: Lee Cantey publishes an installer of Mercurial bos@609: for Mac OS X at http://mercurial.berkwood.com. bos@609: bos@609: bos@609: bos@553: Linux bos@553: bos@584: Because each Linux distribution has its own packaging bos@553: tools, policies, and rate of development, it's difficult to bos@553: give a comprehensive set of instructions on how to install bos@553: Mercurial binaries. The version of Mercurial that you will bos@553: end up with can vary depending on how active the person is who bos@553: maintains the package for your distribution. bos@553: bos@584: To keep things simple, I will focus on installing bos@553: Mercurial from the command line under the most popular Linux bos@553: distributions. Most of these distributions provide graphical bos@553: package managers that will let you install Mercurial with a bos@553: single click; the package name to look for is bos@553: mercurial. bos@553: bos@553: bos@609: Ubuntu and Debian: bos@579: apt-get install mercurial bos@697: Fedora: bos@579: yum install mercurial bos@698: OpenSUSE: bos@697: zypper install mercurial bos@584: Gentoo: bos@553: emerge mercurial bos@553: bos@553: bos@553: bos@553: bos@553: Solaris bos@553: bos@584: SunFreeWare, at http://www.sunfreeware.com, bos@609: provides prebuilt packages of Mercurial. bos@609: bos@609: bos@609: bos@553: bos@609: bos@553: bos@553: Getting started bos@553: bos@584: To begin, we'll use the hg bos@553: version command to find out whether Mercurial is bos@697: installed properly. The actual version information that it bos@697: prints isn't so important; we simply care whether the command bos@697: runs and prints anything at all. bos@559: bos@566: &interaction.tour.version; bos@553: bos@553: bos@553: Built-in help bos@553: bos@584: Mercurial provides a built-in help system. This is bos@559: invaluable for those times when you find yourself stuck bos@559: trying to remember how to run a command. If you are bos@559: completely stuck, simply run hg bos@559: help; it will print a brief list of commands, bos@559: along with a description of what each does. If you ask for bos@559: help on a specific command (as below), it prints more bos@559: detailed information. bos@559: bos@566: &interaction.tour.help; bos@559: bos@584: For a more impressive level of detail (which you won't bos@559: usually need) run hg help . The option is short for bos@559: , and tells bos@559: Mercurial to print more information than it usually bos@559: would. bos@553: bos@553: bos@553: bos@553: bos@553: Working with a repository bos@553: bos@584: In Mercurial, everything happens inside a bos@553: repository. The repository for a project bos@553: contains all of the files that belong to that bos@553: project, along with a historical record of the project's bos@553: files. bos@553: bos@584: There's nothing particularly magical about a repository; it bos@553: is simply a directory tree in your filesystem that Mercurial bos@553: treats as special. You can rename or delete a repository any bos@553: time you like, using either the command line or your file bos@553: browser. bos@553: bos@553: bos@553: Making a local copy of a repository bos@553: bos@584: Copying a repository is just a little bos@553: bit special. While you could use a normal file copying bos@553: command to make a copy of a repository, it's best to use a bos@553: built-in command that Mercurial provides. This command is bos@553: called hg clone, because it bos@609: makes an identical copy of an existing repository. bos@559: bos@566: &interaction.tour.clone; bos@559: bos@676: One advantage of using hg bos@609: clone is that, as we can see above, it lets us clone bos@609: repositories over the network. Another is that it remembers bos@609: where we cloned from, which we'll find useful soon when we bos@609: want to fetch new changes from another repository. bos@609: bos@584: If our clone succeeded, we should now have a local bos@559: directory called hello. bos@559: This directory will contain some files. bos@559: bos@566: &interaction.tour.ls; bos@559: bos@584: These files have the same contents and history in our bos@559: repository as they do in the repository we cloned. bos@553: bos@609: Every Mercurial repository is complete, bos@609: self-contained, and independent. It contains its own private bos@609: copy of a project's files and history. As we just mentioned, bos@609: a cloned repository remembers the location of the repository bos@609: it was cloned from, but Mercurial will not communicate with bos@609: that repository, or any other, unless you tell it to. bos@553: bos@584: What this means for now is that we're free to experiment bos@553: with our repository, safe in the knowledge that it's a private bos@553: sandbox that won't affect anyone else. bos@553: bos@553: bos@553: bos@553: What's in a repository? bos@553: bos@584: When we take a more detailed look inside a repository, we bos@553: can see that it contains a directory named .hg. This is where Mercurial bos@559: keeps all of its metadata for the repository. bos@559: bos@566: &interaction.tour.ls-a; bos@553: bos@584: The contents of the .hg directory and its bos@553: subdirectories are private to Mercurial. Every other file and bos@553: directory in the repository is yours to do with as you bos@553: please. bos@553: bos@584: To introduce a little terminology, the .hg directory is the bos@553: real repository, and all of the files and bos@553: directories that coexist with it are said to live in the bos@553: working directory. An easy way to bos@553: remember the distinction is that the bos@553: repository contains the bos@553: history of your project, while the bos@553: working directory contains a bos@553: snapshot of your project at a particular bos@553: point in history. bos@553: bos@553: bos@553: bos@553: bos@553: A tour through history bos@553: bos@584: One of the first things we might want to do with a new, bos@553: unfamiliar repository is understand its history. The hg log command gives us a view of bos@609: the history of changes in the repository. bos@559: bos@566: &interaction.tour.log; bos@559: bos@584: By default, this command prints a brief paragraph of output bos@559: for each change to the project that was recorded. In Mercurial bos@559: terminology, we call each of these recorded events a bos@553: changeset, because it can contain a record bos@553: of changes to several files. bos@553: bos@584: The fields in a record of output from hg log are as follows. bos@609: bos@553: bos@609: changeset: This bos@609: field has the format of a number, followed by a colon, bos@609: followed by a hexadecimal (or hex) bos@609: string. These are identifiers for the bos@609: changeset. The hex string is a unique identifier: the same bos@697: hex string will always refer to the same changeset in every bos@697: copy of this repository. The bos@609: number is shorter and easier to type than the hex string, bos@609: but it isn't unique: the same number in two different clones bos@697: of a repository may identify different changesets. bos@609: bos@584: user: The identity of the bos@553: person who created the changeset. This is a free-form bos@553: field, but it most often contains a person's name and email bos@553: address. bos@584: date: The date and time on bos@553: which the changeset was created, and the timezone in which bos@553: it was created. (The date and time are local to that bos@553: timezone; they display what time and date it was for the bos@553: person who created the changeset.) bos@584: summary: The first line of bos@553: the text message that the creator of the changeset entered bos@609: to describe the changeset. bos@609: bos@676: Some changesets, such as the first in the list above, bos@609: have a tag field. A tag is another way bos@609: to identify a changeset, by giving it an easy-to-remember bos@609: name. (The tag named tip is special: it bos@609: always refers to the newest change in a repository.) bos@609: bos@609: bos@609: bos@609: The default output printed by hg log is purely a summary; it is bos@609: missing a lot of detail. bos@553: bos@592: provides bos@592: a graphical representation of the history of the hello repository, to make it a bos@553: little easier to see which direction history is bos@553: flowing in. We'll be returning to this figure bos@553: several times in this chapter and the chapter that bos@553: follows. bos@553: bos@591:
bos@591: Graphical history of the <filename bos@591: class="directory">hello</filename> repository bos@558: bos@594: bos@558: XXX add text bos@558: bos@591:
bos@553: bos@553: bos@553: Changesets, revisions, and talking to other bos@553: people bos@553: bos@584: As English is a notoriously sloppy language, and computer bos@553: science has a hallowed history of terminological confusion bos@553: (why use one term when four will do?), revision control has a bos@553: variety of words and phrases that mean the same thing. If you bos@553: are talking about Mercurial history with other people, you bos@553: will find that the word changeset is often bos@553: compressed to change or (when written) bos@553: cset, and sometimes a changeset is referred to bos@553: as a revision or a rev. bos@553: bos@584: While it doesn't matter what word you bos@553: use to refer to the concept of a changeset, the bos@553: identifier that you use to refer to bos@553: a specific changeset is of bos@553: great importance. Recall that the changeset bos@553: field in the output from hg bos@553: log identifies a changeset using both a number and bos@553: a hexadecimal string. bos@553: bos@609: The revision number is a handy bos@609: notation that is only valid in that bos@609: repository. bos@609: The hexadecimal string is the bos@553: permanent, unchanging identifier that bos@553: will always identify that exact changeset in bos@553: every copy of the bos@553: repository. bos@609: bos@609: This distinction is important. If you send bos@609: someone an email talking about revision 33, bos@609: there's a high likelihood that their revision 33 will bos@609: not be the same as yours. The reason for bos@609: this is that a revision number depends on the order in which bos@609: changes arrived in a repository, and there is no guarantee bos@609: that the same changes will happen in the same order in bos@609: different repositories. Three changes a,b,c bos@609: can easily appear in one repository as bos@609: 0,1,2, while in another as bos@609: 0,2,1. bos@553: bos@584: Mercurial uses revision numbers purely as a convenient bos@553: shorthand. If you need to discuss a changeset with someone, bos@553: or make a record of a changeset for some other reason (for bos@553: example, in a bug report), use the hexadecimal bos@553: identifier. bos@553: bos@553: bos@553: bos@553: Viewing specific revisions bos@553: bos@584: To narrow the output of hg bos@553: log down to a single revision, use the (or ) option. You can use bos@609: either a revision number or a hexadecimal identifier, bos@559: and you can provide as many revisions as you want. bos@559: bos@566: &interaction.tour.log-r; bos@553: bos@584: If you want to see the history of several revisions bos@553: without having to list each one, you can use range bos@553: notation; this lets you express the idea I bos@559: want all revisions between abc and bos@559: def, inclusive. bos@559: bos@566: &interaction.tour.log.range; bos@559: bos@584: Mercurial also honours the order in which you specify bos@559: revisions, so hg log -r 2:4 bos@559: prints 2, 3, and 4. while hg log -r bos@559: 4:2 prints 4, 3, and 2. bos@553: bos@553: bos@553: bos@553: More detailed information bos@553: bos@584: While the summary information printed by hg log is useful if you already know bos@553: what you're looking for, you may need to see a complete bos@553: description of the change, or a list of the files changed, if bos@553: you're trying to decide whether a changeset is the one you're bos@553: looking for. The hg log bos@553: command's (or ) option gives you bos@559: this extra detail. bos@559: bos@566: &interaction.tour.log-v; bos@553: bos@592: If you want to see both the description and bos@592: content of a change, add the (or ) option. This displays bos@592: the content of a change as a unified diff bos@592: (if you've never seen a unified diff before, see for an overview). bos@559: bos@566: &interaction.tour.log-vp; bos@553: bos@676: The option is bos@609: tremendously useful, so it's well worth remembering. bos@609: bos@553: bos@553:
bos@609: bos@553: bos@553: All about command options bos@553: bos@584: Let's take a brief break from exploring Mercurial commands bos@553: to discuss a pattern in the way that they work; you may find bos@553: this useful to keep in mind as we continue our tour. bos@553: bos@584: Mercurial has a consistent and straightforward approach to bos@553: dealing with the options that you can pass to commands. It bos@553: follows the conventions for options that are common to modern bos@553: Linux and Unix systems. bos@609: bos@553: bos@609: bos@609: Every option has a long name. For example, as bos@553: we've already seen, the hg bos@553: log command accepts a option. bos@609: bos@609: bos@609: Most options have short names, too. Instead bos@609: of , we can use bos@609: . (The reason that bos@609: some options don't have short names is that the options in bos@609: question are rarely used.) bos@609: bos@609: bos@609: Long options start with two dashes (e.g. bos@609: ), while short bos@609: options start with one (e.g. ). bos@609: bos@609: bos@609: Option naming and usage is consistent across bos@553: commands. For example, every command that lets you specify bos@553: a changeset ID or revision number accepts both and arguments. bos@609: bos@609: bos@676: If you are using short options, you can save typing by bos@609: running them together. For example, the command hg log -v -p -r 2 can be written bos@609: as hg log -vpr2. bos@609: bos@609: bos@609: bos@697: In the examples throughout this book, I usually bos@697: use short options instead of long. This simply reflects my own bos@697: preference, so don't read anything significant into it. bos@553: bos@584: Most commands that print output of some kind will print more bos@553: output when passed a bos@553: (or ) option, and bos@553: less when passed (or bos@553: ). bos@553: bos@609: bos@609: Option naming consistency bos@609: bos@676: Almost always, Mercurial commands use consistent option bos@609: names to refer to the same concepts. For instance, if a bos@609: command deals with changesets, you'll always identify them bos@609: with or . This consistent use of bos@609: option names makes it easier to remember what options a bos@609: particular command takes. bos@609: bos@609: bos@553: bos@553: bos@553: Making and reviewing changes bos@553: bos@584: Now that we have a grasp of viewing history in Mercurial, bos@553: let's take a look at making some changes and examining bos@553: them. bos@553: bos@584: The first thing we'll do is isolate our experiment in a bos@553: repository of its own. We use the hg bos@553: clone command, but we don't need to clone a copy of bos@553: the remote repository. Since we already have a copy of it bos@553: locally, we can just clone that instead. This is much faster bos@553: than cloning over the network, and cloning a local repository bos@609: uses less disk space in most cases, too bos@676: The saving of space arises when source and destination bos@609: repositories are on the same filesystem, in which case bos@609: Mercurial will use hardlinks to do copy-on-write sharing of bos@609: its internal metadata. If that explanation meant nothing to bos@609: you, don't worry: everything happens transparently and bos@609: automatically, and you don't need to understand it. bos@609: . bos@559: bos@566: &interaction.tour.reclone; bos@559: bos@584: As an aside, it's often good practice to keep a bos@559: pristine copy of a remote repository around, bos@559: which you can then make temporary clones of to create sandboxes bos@559: for each task you want to work on. This lets you work on bos@559: multiple tasks in parallel, each isolated from the others until bos@559: it's complete and you're ready to integrate it back. Because bos@559: local clones are so cheap, there's almost no overhead to cloning bos@559: and destroying repositories whenever you want. bos@553: bos@584: In our my-hello bos@553: repository, we have a file hello.c that bos@609: contains the classic hello, world program. bos@609: bos@609: &interaction.tour.cat1; bos@609: bos@676: Let's edit this file so that it prints a second line of bos@609: output. bos@609: bos@609: &interaction.tour.cat2; bos@553: bos@584: Mercurial's hg status bos@553: command will tell us what Mercurial knows about the files in the bos@559: repository. bos@559: bos@566: &interaction.tour.status; bos@559: bos@584: The hg status command bos@559: prints no output for some files, but a line starting with bos@553: M for bos@553: hello.c. Unless you tell it to, hg status will not print any output bos@553: for files that have not been modified. bos@553: bos@584: The M indicates that bos@553: Mercurial has noticed that we modified bos@553: hello.c. We didn't need to bos@553: inform Mercurial that we were going to bos@553: modify the file before we started, or that we had modified the bos@553: file after we were done; it was able to figure this out bos@553: itself. bos@553: bos@609: It's somewhat helpful to know that we've modified bos@553: hello.c, but we might prefer to know bos@553: exactly what changes we've made to it. To bos@553: do this, we use the hg diff bos@559: command. bos@559: bos@566: &interaction.tour.diff; bos@553: bos@609: bos@609: Understanding patches bos@609: bos@676: Remember to take a look at if you don't know how to read bos@609: output above. bos@609: bos@553: bos@553: bos@553: Recording changes in a new changeset bos@553: bos@584: We can modify files, build and test our changes, and use bos@553: hg status and hg diff to review our changes, until bos@553: we're satisfied with what we've done and arrive at a natural bos@553: stopping point where we want to record our work in a new bos@553: changeset. bos@553: bos@584: The hg commit command lets bos@553: us create a new changeset; we'll usually refer to this as bos@553: making a commit or bos@553: committing. bos@553: bos@553: bos@553: Setting up a username bos@553: bos@584: When you try to run hg bos@553: commit for the first time, it is not guaranteed to bos@553: succeed. Mercurial records your name and address with each bos@553: change that you commit, so that you and others will later be bos@553: able to tell who made each change. Mercurial tries to bos@553: automatically figure out a sensible username to commit the bos@553: change with. It will attempt each of the following methods, bos@553: in order: bos@553: bos@584: If you specify a option to the hg commit command on the command bos@553: line, followed by a username, this is always given the bos@553: highest precedence. bos@584: If you have set the HGUSER bos@553: environment variable, this is checked bos@553: next. bos@592: If you create a file in your home bos@592: directory called .hgrc, with a username entry, that will be bos@592: used next. To see what the contents of this file should bos@592: look like, refer to bos@553: below. bos@584: If you have set the EMAIL bos@553: environment variable, this will be used bos@553: next. bos@584: Mercurial will query your system to find out bos@553: your local user name and host name, and construct a bos@553: username from these components. Since this often results bos@553: in a username that is not very useful, it will print a bos@553: warning if it has to do bos@558: this. bos@558: bos@584: If all of these mechanisms fail, Mercurial will bos@553: fail, printing an error message. In this case, it will not bos@553: let you commit until you set up a bos@558: username. bos@584: You should think of the HGUSER environment bos@558: variable and the bos@558: option to the hg commit bos@558: command as ways to override Mercurial's bos@558: default selection of username. For normal use, the simplest bos@558: and most robust way to set a username for yourself is by bos@558: creating a .hgrc file; see bos@558: below for details. bos@558: bos@553: Creating a Mercurial configuration file bos@558: bos@674: To set a user name, use your favorite editor bos@697: to create a file called .hgrc in your home directory. bos@697: Mercurial will use this file to look up your personalised bos@697: configuration settings. The initial contents of your bos@697: .hgrc should look like bos@697: this. bos@697: bos@697: bos@697: <quote>Home directory</quote> on Windows bos@697: bos@698: When we refer to your home directory, on an English bos@697: language installation of Windows this will usually be a bos@697: folder named after your user name in bos@697: C:\Documents and Settings. You can bos@697: find out the exact name of your home directory by opening bos@697: a command prompt window and running the following bos@697: command. bos@697: bos@697: C:\> echo %UserProfile% bos@697: bos@609: bos@558: # This is a Mercurial configuration file. bos@579: [ui] bos@697: username = Firstname Lastname <email.address@example.net> bos@558: bos@584: The [ui] line begins a bos@558: section of the config file, so you can bos@558: read the username = ... bos@558: line as meaning set the value of the bos@558: username item in the bos@558: ui section. A section continues bos@558: until a new section begins, or the end of the file. bos@558: Mercurial ignores empty lines and treats any text from bos@558: # to the end of a line as bos@558: a comment. bos@553: bos@558: bos@553: bos@553: Choosing a user name bos@553: bos@584: You can use any text you like as the value of bos@697: the username config item, since this bos@697: information is for reading by other people, but will not be bos@697: interpreted by Mercurial. The convention that most people bos@697: follow is to use their name and email address, as in the bos@697: example above. bos@553: bos@584: Mercurial's built-in web server obfuscates bos@697: email addresses, to make it more difficult for the email bos@697: harvesting tools that spammers use. This reduces the bos@697: likelihood that you'll start receiving more junk email if bos@697: you publish a Mercurial repository on the bos@697: web. bos@553: bos@553: bos@553: bos@553: Writing a commit message bos@553: bos@584: When we commit a change, Mercurial drops us into bos@697: a text editor, to enter a message that will describe the bos@697: modifications we've made in this changeset. This is called bos@697: the commit message. It will be a record bos@697: for readers of what we did and why, and it will be printed by bos@697: hg log after we've finished bos@697: committing. bos@558: bos@566: &interaction.tour.commit; bos@558: bos@584: The editor that the hg bos@697: commit command drops us into will contain an empty bos@697: line or two, followed by a number of lines starting with bos@697: HG:. bos@558: bos@609: bos@609: This is where I type my commit comment. bos@609: bos@609: HG: Enter commit message. Lines beginning with 'HG:' are removed. bos@609: HG: -- bos@609: HG: user: Bryan O'Sullivan <bos@serpentine.com> bos@609: HG: branch 'default' bos@609: HG: changed hello.c bos@558: bos@584: Mercurial ignores the lines that start with bos@697: HG:; it uses them only to bos@697: tell us which files it's recording changes to. Modifying or bos@697: deleting these lines has no effect. bos@553: bos@553: bos@553: Writing a good commit message bos@553: bos@584: Since hg log bos@697: only prints the first line of a commit message by default, bos@697: it's best to write a commit message whose first line stands bos@697: alone. Here's a real example of a commit message that bos@697: doesn't follow this guideline, and hence bos@697: has a summary that is not readable. bos@558: bos@558: bos@558: changeset: 73:584af0e231be bos@579: user: Censored Person <censored.person@example.org> bos@579: date: Tue Sep 26 21:37:07 2006 -0700 bos@558: summary: include buildmeister/commondefs. Add exports. bos@558: bos@584: As far as the remainder of the contents of the bos@697: commit message are concerned, there are no hard-and-fast bos@697: rules. Mercurial itself doesn't interpret or care about the bos@697: contents of the commit message, though your project may have bos@697: policies that dictate a certain kind of formatting. bos@584: My personal preference is for short, but bos@697: informative, commit messages that tell me something that I bos@697: can't figure out with a quick glance at the output of hg log --patch. bos@697: If we run the hg bos@697: commit command without any arguments, it records bos@697: all of the changes we've made, as reported by hg status and hg diff. bos@697: bos@697: bos@697: A surprise for Subversion users bos@697: bos@698: Like other Mercurial commands, if we don't supply bos@697: explicit names to commit to the hg bos@697: commit, it will operate across a repository's bos@697: entire working directory. Be wary of this if you're coming bos@697: from the Subversion or CVS world, since you might expect it bos@697: to operate only on the current directory that you happen to bos@697: be visiting and its subdirectories. bos@697: bos@697: bos@697: bos@553: bos@553: Aborting a commit bos@553: bos@584: If you decide that you don't want to commit bos@697: while in the middle of editing a commit message, simply exit bos@697: from your editor without saving the file that it's editing. bos@697: This will cause nothing to happen to either the repository or bos@697: the working directory. bos@697: bos@697: bos@553: bos@553: Admiring our new handiwork bos@553: bos@584: Once we've finished the commit, we can use the bos@697: hg tip command to display the bos@697: changeset we just created. This command produces output that bos@697: is identical to hg log, but bos@697: it only displays the newest revision in the repository. bos@558: bos@566: &interaction.tour.tip; bos@558: bos@609: We refer to the newest revision in the bos@609: repository as the tip revision, or simply bos@609: the tip. bos@609: bos@676: By the way, the hg tip bos@609: command accepts many of the same options as hg log, so above indicates be bos@609: verbose, bos@609: specifies print a patch. The use of to print patches is another bos@609: example of the consistent naming we mentioned earlier. bos@553: bos@553: bos@558: bos@553: bos@553: Sharing changes bos@553: bos@584: We mentioned earlier that repositories in bos@697: Mercurial are self-contained. This means that the changeset we bos@697: just created exists only in our my-hello repository. Let's look bos@697: at a few ways that we can propagate this change into other bos@697: repositories. bos@558: bos@558: bos@553: Pulling changes from another repository bos@697: bos@584: To get started, let's clone our original bos@697: hello repository, which bos@697: does not contain the change we just committed. We'll call our bos@697: temporary repository hello-pull. bos@558: bos@566: &interaction.tour.clone-pull; bos@558: bos@584: We'll use the hg bos@697: pull command to bring changes from my-hello into hello-pull. However, blindly bos@697: pulling unknown changes into a repository is a somewhat scary bos@697: prospect. Mercurial provides the hg bos@697: incoming command to tell us what changes the bos@697: hg pull command bos@697: would pull into the repository, without bos@697: actually pulling the changes in. bos@558: bos@566: &interaction.tour.incoming; bos@558: bos@584: Bringing changes into a repository is a simple bos@697: matter of running the hg pull bos@697: command, and optionally telling it which repository to pull from. bos@558: bos@566: &interaction.tour.pull; bos@558: bos@697: As you can see from the before-and-after output bos@697: of hg tip, we have bos@697: successfully pulled changes into our repository. However, bos@697: Mercurial separates pulling changes in from updating the bos@697: working directory. There remains one step before we will see bos@697: the changes that we just pulled appear in the working bos@697: directory. bos@697: bos@697: bos@697: Pulling specific changes bos@697: bos@697: It is possible that due to the delay between bos@697: running hg incoming and bos@697: hg pull, you may not see bos@697: all changesets that will be brought from the other bos@697: repository. Suppose you're pulling changes from a repository bos@697: on the network somewhere. While you are looking at the bos@697: hg incoming output, and bos@697: before you pull those changes, someone might have committed bos@697: something in the remote repository. This means that it's bos@697: possible to pull more changes than you saw when using bos@697: hg incoming. bos@697: bos@698: If you only want to pull precisely the changes that were bos@697: listed by hg incoming, or bos@697: you have some other reason to pull a subset of changes, bos@697: simply identify the change that you want to pull by its bos@697: changeset ID, e.g. hg pull bos@697: -r7e95bb. bos@697: bos@697: bos@697: bos@553: bos@553: Updating the working directory bos@553: bos@592: We have so far glossed over the relationship bos@592: between a repository and its working directory. The hg pull command that we ran in bos@592: brought changes into the bos@592: repository, but if we check, there's no sign of those changes bos@592: in the working directory. This is because hg pull does not (by default) touch bos@559: the working directory. Instead, we use the hg update command to do this. bos@559: bos@566: &interaction.tour.update; bos@559: bos@697: It might seem a bit strange that hg pull doesn't update the working bos@697: directory automatically. There's actually a good reason for bos@697: this: you can use hg update bos@697: to update the working directory to the state it was in at bos@697: any revision in the history of the bos@697: repository. If you had the working directory updated to an bos@697: old revision&emdash;to hunt down the origin of a bug, bos@697: say&emdash;and ran a hg pull bos@697: which automatically updated the working directory to a new bos@697: revision, you might not be terribly happy. bos@697: bos@697: Since pull-then-update is such a common sequence bos@697: of operations, Mercurial lets you combine the two by passing bos@697: the option to hg pull. bos@558: bos@584: If you look back at the output of hg pull in when we ran it without , you can see that it printed bos@559: a helpful reminder that we'd have to take an explicit step to bos@699: update the working directory. bos@558: bos@697: To find out what revision the working directory bos@697: is at, use the hg parents bos@559: command. bos@558: bos@566: &interaction.tour.parents; bos@558: bos@592: If you look back at , you'll see arrows bos@697: connecting each changeset. The node that the arrow leads bos@697: from in each case is a parent, and the bos@697: node that the arrow leads to is its bos@697: child. The working directory has a parent in just the same bos@697: way; this is the changeset that the working directory bos@697: currently contains. bos@697: bos@697: To update the working directory to a particular bos@697: revision, give a revision number or changeset ID to the bos@697: hg update command. bos@559: bos@566: &interaction.tour.older; bos@559: bos@584: If you omit an explicit revision, hg update will update to the tip bos@559: revision, as shown by the second call to hg update in the example bos@559: above. bos@558: bos@558: bos@553: bos@553: Pushing changes to another repository bos@553: bos@584: Mercurial lets us push changes to another bos@697: repository, from the repository we're currently visiting. As bos@697: with the example of hg pull bos@697: above, we'll create a temporary repository to push our changes bos@697: into. bos@558: bos@566: &interaction.tour.clone-push; bos@558: bos@697: The hg outgoing bos@697: command tells us what changes would be pushed into another bos@697: repository. bos@558: bos@566: &interaction.tour.outgoing; bos@558: bos@697: And the hg push bos@697: command does the actual push. bos@558: bos@566: &interaction.tour.push; bos@558: bos@609: As with hg bos@609: pull, the hg push bos@609: command does not update the working directory in the bos@609: repository that it's pushing changes into. Unlike hg pull, hg bos@609: push does not provide a -u bos@609: option that updates the other repository's working directory. bos@609: This asymmetry is deliberate: the repository we're pushing to bos@609: might be on a remote server and shared between several people. bos@609: If we were to update its working directory while someone was bos@609: working in it, their work would be disrupted. bos@558: bos@584: What happens if we try to pull or push changes bos@553: and the receiving repository already has those changes? bos@558: Nothing too exciting. bos@558: bos@566: &interaction.tour.push.nothing; bos@553: bos@697: bos@697: bos@697: Default locations bos@697: bos@698: When we clone a repository, Mercurial records the location bos@697: of the repository we cloned in the bos@697: .hg/hgrc file of the new repository. If bos@697: we don't supply a location to hg pull from bos@697: or hg push to, those commands will use this bos@697: location as a default. The hg incoming bos@697: and hg outgoing commands do so too. bos@697: bos@698: If you open a repository's .hg/hgrc bos@697: file in a text editor, you will see contents like the bos@697: following. bos@697: bos@697: [paths] bos@697: default = http://www.selenic.com/repo/hg bos@697: bos@698: It is possible&emdash;and often useful&emdash;to have the bos@697: default location for hg push and bos@697: hg outgoing be different from those for bos@697: hg pull and hg incoming. bos@697: We can do this by adding a default-push bos@697: entry to the [paths] section of the bos@697: .hg/hgrc file, as follows. bos@697: bos@697: [paths] bos@697: default = http://www.selenic.com/repo/hg bos@697: default-push = http://hg.example.com/hg bos@697: bos@697: bos@553: bos@553: Sharing changes over a network bos@553: bos@584: The commands we have covered in the previous few bos@553: sections are not limited to working with local repositories. bos@553: Each works in exactly the same fashion over a network bos@558: connection; simply pass in a URL instead of a local bos@558: path. bos@558: bos@566: &interaction.tour.outgoing.net; bos@558: bos@697: In this example, we can see what changes we bos@697: could push to the remote repository, but the repository is bos@697: understandably not set up to let anonymous users push to bos@697: it. bos@558: bos@566: &interaction.tour.push.net; bos@553: bos@553: bos@699: bos@699: bos@699: Starting a new project bos@699: bos@700: It is just as easy to begin a new project as to work on one bos@699: that already exists. The hg init command bos@699: creates a new, empty Mercurial repository. bos@699: bos@699: &interaction.ch01-new.init; bos@699: bos@700: This simply creates a repository named bos@699: myproject in the current directory. bos@699: bos@699: &interaction.ch01-new.ls; bos@699: bos@700: We can tell that myproject is a bos@699: Mercurial repository, because it contains a bos@699: .hg directory. bos@699: bos@699: &interaction.ch01-new.ls2; bos@699: bos@700: If we want to add some pre-existing files to the repository, bos@699: we copy them into place, and tell Mercurial to start tracking bos@699: them using the hg add command. bos@699: bos@699: &interaction.ch01-new.add; bos@699: bos@700: Once we are satisfied that our project looks right, we bos@699: commit our changes. bos@699: bos@699: &interaction.ch01-new.commit; bos@699: bos@700: It takes just a few moments to start using Mercurial on a bos@699: new project, which is part of its appeal. Revision control is bos@699: now so easy to work with, we can use it on the smallest of bos@699: projects that we might not have considered with a more bos@699: complicated tool. bos@699: bos@553:
bos@553: bos@553: