Mercurial tips

Contents

Overview

Mercurial is a revision control system. That means it is designed to help track work on a collection of files, giving you the ability to identify and retrieve the revisions that existed at various points during the process. The huge motivation for revision control is when there are several people all working on the same program (or collection of programs), because then it is very helpful to be able to fugure out what was changed and by whom. But revision control is also helpful when you are working by yourself: if you manage to make a mess, you can back up to a coherent prior state.

Mercurial can do much more than we need in this class. If you get really interested, or you need something more, there is a very good online tutorial and reference, This page is limited to the features you are most likely to need in this class.

General ideas

Mercurial manages a subtree of files: a directory and all of its subdirectories. This matches the way we usually work on projects (including labs): we make a new directory for each new project. When we are working in a Mercurial-managed tree, we see the files there as we would normally. But Mecurial also maintains, behind the scenes, additional files that keep the history and other information; we call that hidden collection the repository. (The repository is actually hiding in a dirctory named .hg, taking advantage of the fact that names beginning with dot are nor normally shown.) The visible files we actually work with are called a working copy. We manage the repository and its relationship with the working copy using the hg commmand.

The main Mercurial command hg is always followed by another command that tells Mercurial what to do. It is very helpful to remember that one of those commands is help. So you can always do

$ hg help
to get general help, or add a command, such as update, to the end of the help to get a reminder of what that command does and what its arguments can be; for example,
$ hg help update
or even
$ hg help help

Setup and customization

Before you can make very much use of Mercurial, you need to do a tiny bit of setting up (which has already been done for the lab accounts, and in rudimentary form for the accounts of students in this class). Whenever you run hg, it will look for a file named .hgrc in your home directory for some settings and options. You can add to that later on if you want to set some more options.

Repositories

In this class, you'll often start from a repository that already exists; you'll create your own working copy and repository by cloning the existing repository. When that repository is local, you can name it with its filename; so the command

$ hg clone /cslab/class/csci245/lab4 .
will clone the repository under /cslab/class/csci245/lab4 to the current working directory (that's the dot); it will make a new directory named lab4 to hold the working copy and repository.

If you already have a directory for which you would like to start using Mercurial, you can create a repository by (1) making sure that you are in the top directory of the tree you want to manage and then (2) issuing the command

$ hg init

When you create a repository, you will need to remember to use the hg add command to tell the repository what files it should manage (see below).

Because there are usually files in the working copy that you don't want in the repository (such as the compiled versions of the source files), you can use a file .hgignore to tell Mercurial the filenames it should ignore. That will be included when you clone a repository, but you can edit it if you need to. That file has patterns in it (a lesson in itself), but you can get a reasonable starting file for this class by copying the file found at

/cslab/class/csci245/misc/dot.hgignore
That file ignores .class files, XEmacs backups, and dot-files. You will want to remember to explicitly add the .hgignore file, because it will otherwise be ignored.

The main commands

These are the commands you will use the most.

Things are pretty easy if you only revert uncommitted files (to their latest committed version) or if you revert the entire tree to a previously committed state. If you want to revert a file to an earlier committed state, you will have to do some merging, which is a subject for another day.

Copying between accounts and/or computers

There are special ways to copy a directory with a Mercurial repository. If user msnerd wants to copy from the lab account to directory csci245/lab4 under his home directory, the command would be

$ hg clone . ssh://msnerd@cslab25/csci245/lab4

You'll be prompted for your password.

When you have logged in to your account and change into your csci245/lab4 directory, all that will be there is the repository. To set up the working copy, you use

$ hg update
which makes the working copy match the last committed version from the repository.
Cary Gray

Last modified: Thu Feb 7 11:53:46 CST 2013

Valid HTML 4.01!