Version control with mercurial

(2014-10-30, update 2016-09-14)

The main open source choices for source code version control systems (VCS) are the centralized VCS CVS and subversion (svn), and the distributed VCS git and Mercurial (hg).

I am taking sides with mercurial, as it is distributed and has a more intuitive command line interface and better portability than git.

Linus Torvalds at Google in 2007: "Subversion has been the most pointless project ever started... Subversion used to say, 'CVS done right.' With that slogan there is nowhere you can go. There is no way to do CVS right." Probably he is not completely wrong. As from this choice only git and mercurial are distributed version control systems, the choice is between them, unless you are using some features of svn they don't provide (e.g. checking out subdirectories).

Please be aware that git is more widespread and it seems that for this reason there are more tools on top of it. Some more colloquial take on the comparison between them you find here.

Introduction

Mercurial (hg) is implemented in python and works on most modern operating systems (Linux, Windows, Mac).

Before starting, create a file named .hgrc in your home directory containing

[ui]
username = Ann User <ann.user@example.com>

In Mercurial, unlike in subversion there is no division between "sandbox" and "repository". Any instance you work with contains both. So in every "sandbox" there is a hidden "repository" (in the .hg subdirectory of the top level direcrory).

You can clone any given instance. Clone commands can using several kinds of access

hg clone http://some.host.atyourinstitute.de:port directoy_to_be_created hg clone file://some_directory_on_your_disc directoy_to_be_created hg clone ssh://some_directory_on_your_remote directoy_to_be_created

There is no a prioi defined master repository. All descendants of the initial repository have the same rights and allow transfer operations between any of them. Therefore it is possible that two people work together using hg and after finishing their joint work, they release it to the others in the project. This is believed to be the main advantage of distributed version control in opposite to the centralized model represented by svn.

Installation

If the command hg is not available on your system, install mercurial using your favorite package manager.

Basic commands

Yes, there are also branches.

Master repository workflow

The structure of Mercurial allows to define different workflow patterns (git version). The simplest and most obvious one for people coming from CVS or svn is a workflow around one central master repository.

The pattern goes as follows:

  1. Clone the master repository and change into directory.

hg clone url localdir
  cd localdir
  1. Pull the latest changes from the master:

hg pull
  hg update
  hg merge
  1. Modify/merge the code. You can commit intermediate changes

without affecting the master repository with

hg commit

This command asks for a short commit message. To add new files to the VCS, before committing use

hg add filename
  1. Test the code. If changes are necessary, repeat step 3.

  2. Repeat step 2 to be sure to have the latest updates.

  3. Test again, to be sure that the most recent changes form the master are compatible with your

work.

  1. Push the changes to the remote repository

hg push

Notification

The maintainer of the master repository has the possibility to configure email notifications.