Never since the invention of agriculture has a human activity revolved so much around the accumulation of labour than... code writing. Fortunately, in contrast to grain, software does not rot. Or does it?
Well, have you ever deleted some important files by mistake? I certainly did. And I still remember the sheer feeling of horror piercing through my heart when realizing my mistake. Inadvertent deletion is just one of the least refined ways of wasting hours of coding in the blink of an eye, here are some other:
- you try to add yet another small feature to a complex program and suddenly nothing works anymore,
- you factor two seemingly identical methods, and some test breaks,
- you remove some obviously useless redrawing code and three days later some weird behaviour pops up...
In any of these cases, don't you wish to simply reverse time, go back just before it broke and forget all about it?
That is exactly what source control systems are for: they are "the granaries of software".
That is exactly what source control systems are for: they are "the granaries of software".

Today, I will introduce my personal favorite source control system: Mercurial. Written in Python, Mercurial is available both for Windows and Unix systems. Under Windows, my advice is rather to download the graphical interface TortoiseHg. All the following examples were executed in a windows shell (cmd.exe) with TortoiseHg installed. The Mercurial commands would still be indentical in any other configuration.
First, in order to check the correct installation of Mercurial, let us type:hg --versionThis should display a version number and copyright notice. Now make an empty directory and enter it:
mkdir hg-projectTo initialize a new repository simply type:
cd hg-project
hg initThis creates a subdirectory .hg in which Mercurial keeps all information about the repository. Let us add a dummy file dummy.txt with some text:
echo "Hello Mercurial!" > dummy.txtNow, if you type:
hg statusMercurial displays the status of the files present in the repository. Right now, there is only one file dummy.txt which is not tracked. This is indicated by a question mark next to the file name, such as this:
? dummy.txtTo track the file, first type:
hg add dummy.txtThe status (obtained by typing the command hg status) becomes:
A dummy.txtwhich means dummy.txt is marked for addition in the next revision.
To create a new revision, simply type:
hg commit --user "James Hacker" --message "[add] First revision of the repository. Added a dummy file for explanatory purposes."
That is where the real fun begins! Inadvertent deletion of tracked files are not to be feared anymore:
del dummy.txtThe status of the repository indicates that dummy.txt is missing. This is displayed with an exclamation mark next to the file name:
! dummy.txtIn order to recover the lost file, just type:
hg revert --all
Another scenario is when you code for a few day, but end up disappointed with the result and want to go back to your initial state. To simulate this scenario let us first replace the text in dummy.txt:
echo "By doing this, I am taking the wrong path" > dummy.txt
M dummy.txtWe commit this modification to the repository:
hg commit --user "James Hacker" --mesage "[add] Performed some experiment. This may turn out to be a mistake..."
hg historyIt should display a list of revisions, each with its number, author, date of creation and description. We want to go back to the first revision (numbered 0), so we type:
hg update --rev 0And voila! If we check the content of dummy.txt, it is now back to its initial content:
more dummy.txtFrom there, we could add a new file to the repository and perform another commit:
echo "Tracking a second file." > another-dummy.txt
hg add another-dummy.txt
hg commit --user "James Hacker" --message "[add] A second dummy file."
hg parent
hg update --rev 1
hg parent
Rome was not built in a day, and so is great software. A source control system such as Mercurial is the granary that protects your code from disasters during development time.
This post presented the basics of Mercurial. In a later post, I will talk about the Mercurial graphical user interface TortoiseHg, explain how default settings are changed and share my personal practices for revision messages.
Some questions for today:
- Do you use a source control system?
- If not, why?
- If so, what is your preferred source control system?