Mercurial for Beginners: The Definitive Practical Guide

Viewed 32837

Inspired by Git for beginners: The definitive practical guide.

This is a compilation of information on using Mercurial for beginners for practical use.

Beginner - a programmer who has touched source control without understanding it very well.

Practical - covering situations that the majority of users often encounter - creating a repository, branching, merging, pulling/pushing from/to a remote repository, etc.

Notes:

  • Explain how to get something done rather than how something is implemented.
  • Deal with one question per answer.
  • Answer clearly and as concisely as possible.
  • Edit/extend an existing answer rather than create a new answer on the same topic.
  • Please provide a link to the the Mercurial wiki or the HG Book for people who want to learn more.

Questions:

Installation/Setup

Working with the code

Tagging, branching, releases, baselines

Other

Other Mercurial references

22 Answers

How do you configure it to ignore files?

Ignore is configured in a normal text file called .hgignore in the root of your repository. Add it just like a normal file with:

hg add .hgignore

There are two syntax options available for file matching, glob and regexp. glob is unix-like filename expansion and regexp is regular expressions. You activate each by adding syntax: glob or syntax: regexp on a line by itself. All lines following that will use that syntax, until the next syntax marker. You can have as many syntax markers as you want. The default syntax is regexp, so if you only use regexp you don't need any syntax marker.

You can add comments with #

Example:

# python temporary files
syntax: glob
*.pyc

#editor autosaves
*~

# temporary data
syntax: regexp
temp

Ignore only applies to unmanaged files (i.e. files that are not already checked in). To ignore files that are under version control, you can use the switches -I and -X.

How do you see what's uncommitted, or the status of your current codebase?

To see a list of files that have been changed:

$ hg status

This will print each file that has been changed along with its status, which can include:

  • M - Modified. The file has been changed and the changes have not been committed.
  • A - Added. The file was not tracked before, but if you commit Mercurial will begin tracking it.
  • R - Removed. The file was tracked before, but if you commit Mercurial will cease tracking it in this and future commits.
  • ? - Unknown. The file is not currently tracked by Mercurial. Committing will have no effect on it unless you use hg add to add it.
  • ! - Missing. The file was tracked but Mercurial cannot find it in the working copy.

To see the changes that have actually been made to the files:

$ hg diff

How do you create a new project/repository?

$ hg init my-repository

How do you branch?

$ hg branch my-branch

or

$ hg clone original-repository my-branch

Though it should be noted that branch creates a "virtual" directory (i.e., the files stay the same, but hg treats them as if they were different inside the system), while clone creates an actual, complete copy. Strictly speaking, clone isn't branching.

How do you 'mark' 'tag' or 'release' a particular set of revisions for a particular set of files so you can always pull that one later?

$ hg tag my-tag

You can also clone your repository to create a special tag repository.

$ hg clone working-repository my-tag-repository

How do you merge branches?

$ cd repository-where-i-want-to merge
$ hg pull branch-i-want-to-merge
$ hg merge # if necessary

How to install Mercurial?

Please edit nicely if you have installed from source on Linux, or used the Windows installers.

Mac OS X 10.4 (Tiger), 10.5 (Leopard)

Use Python's easy_install (with Setuptools):

sudo easy_install mercurial

This finds the latest version (1.3.1 at time of writing) and installs at:

/Library/Frameworks/Python.framework/Versions/2.6/bin/

With Python 2.6 this also gets around the Mercurial OS X installer package (at 1.2.1 as of July 26 2009) complaining that it needs Python 2.5. From the documentation, it appears that Fink and Macports install version 1.2.

Linux

Most of the explicit Linux packages appear to lag behind the current version, so use easy_install (as above) or download the Mercurial tarball, extract the archive, change to the mercurial directory, and run:

$ make
$ sudo make install    # do a system-wide install
$ hg debuginstall      # sanity check
$ hg                   # see help

(from Introducing Mercurial, a distributed version control system)

Windows

There is a binary package of the latest version of Mercurial. TortoiseHg is a Windows shell extension for, and installs, Mercurial. Cygwin can also install Mercurial.

Alternatively (instructions too lengthy so linked here), you can build an optimised or pure Python version of Mercurial from source.

How do you get the latest code?

Mercurial remembers where a repository was cloned from (in .hg/hgrc) so you can simply run:

hg pull

to pull the latest code from origin-repository. (This does not update the working directory)

hg update

to update the working directory.

hg pull -u

to perform both a pull and an update at once.

Related