Mercurial (1/2)

Introduction

Mercurial is distributed version control system. Its website is here.

Installing Mercurial

Windows installation

Mercurial can be downloaded from the download page on its website: http://mercurial.selenic.com/downloads/. You have the choice between several packages including one that comes with TortoiseHg and is therefore probably the easiest to use on Windows.

Note that you don't need to install Python separately: the Windows packages use their own embedded Python interpreter. On other download pages you may come across a package to install Mercurial as Python modules, this is mainly for publishing repositories with a web server so you shouldn't use that version if you just want to work with a repository.

The installation is as trivial as launching the installer and clicking a few buttons so we won't discuss it further here.

Creating a repository and adding files to it

Now that Mercurial is installed we are ready to create our first repository. Open a command prompt and type the following command. We have chosen to place the new repository in the c:\MercurialRepositories\MyFirstRepository path.

Creating a new repository
c:\>hg init c:\MercurialRepositories\MyFirstRepository

If we navigate to this directory we notice that it already contains a subdirectory named .hg. This is where Mercurial keeps all the metadata required by the repository and you should obviously not interfere with it unless you know what you are doing.

Let's now add a file to our directory. With Mercurial the repository is also where the working copy is located. There isn't really a concept of creating a client view and checking out the code into that client view like you may be accustomed to with other version control systems. So to add our file to the repository we add it directly to the c:\MercurialRepositories\MyFirstRepository. Let's call it "HelloWorld.txt" and it has the following contents.

File: c:\MercurialRepositories\MyFirstRepository\HelloWorld.txt
Hello World!

We run the following command to tell Mercurial to track the file. This doesn't add the file to the repository yet, we still need to do a commit.

Adding the file
c:\MercurialRepositories\MyFirstRepository>hg add
adding HelloWorld.txt

c:\MercurialRepositories\MyFirstRepository>

Now that the file is tracked by Mercurial we can commit it by running the command below. We need to specify a username for the commit. Note that at no point we have specified a list of approved users or any passwords so you could actually at this stage specify any username but a username is required when doing a commit. Later on we will see how you can avoid having to specify the "-u" argument everytime by using a configuration file.

Committing the changelist
c:\MercurialRepositories\MyFirstRepository>hg commit -m "Initial commit" -u xleclercq

c:\MercurialRepositories\MyFirstRepository>

The command didn't print any error messages so it was successful. We can view the list of changes by running the following command.

Viewing the commit log
c:\MercurialRepositories\MyFirstRepository>hg log
changeset:   0:a0665fff41de
tag:         tip
user:        xleclercq
date:        Tue Nov 05 21:48:08 2013 +0000
summary:     Initial commit

Editing files

Now that we have a file in the repository let's make a change to it. Open the HelloWorld.txt file in a text editor and modify it as follows.

File: c:\MercurialRepositories\MyFirstRepository\HelloWorld.txt
Hello World!
It's a beautiful day.

You can now run hg status and it will list HelloWorld.txt as having been modified (indicated by the 'M' before the file). Note that we didn't have to tell Mercurial that we changed the file, it automatically detected the change.

After running hg status we also ran hg diff to display the changes before using hg commit as we did previously to commit the changes.

Making a change to a file in the repository
c:\MercurialRepositories\MyFirstRepository>hg status
M HelloWorld.txt

c:\MercurialRepositories\MyFirstRepository>hg diff
diff -r a0665fff41de HelloWorld.txt
--- a/HelloWorld.txt    Tue Nov 05 21:48:08 2013 +0000
+++ b/HelloWorld.txt    Wed Nov 06 00:07:03 2013 +0000
@ -1,1 +1,2 @
-Hello World!
\ No newline at end of file
+Hello World!
+It's a beautiful day.
\ No newline at end of file

c:\MercurialRepositories\MyFirstRepository>hg commit -m "Updated HelloWorld.txt" -u xleclercq

If we now issue the hg log command again it will show two changes.

Viewing the commit log
c:\MercurialRepositories\MyFirstRepository>hg log
changeset:   1:c532bb39f0a2
tag:         tip
user:        xleclercq
date:        Wed Nov 06 00:08:05 2013 +0000
summary:     Updated HelloWorld.txt

changeset:   0:a0665fff41de
user:        xleclercq
date:        Tue Nov 05 21:48:08 2013 +0000
summary:     Initial commit

blog comments powered by Disqus

Copyright(c) 2006-2017 Xavier Leclercq | Privacy policy

Home
Contact Us
Search