Skip to content

Learning how to use HG / Mercurial

April 10, 2011

I just started working on a website redesign for a non-profit organization and the first thing I do before making any changes is saving the current source code and theme files to a version-control system. I have used SVN and Git in the past, so I thought I would give HG a try. Along the way I found some basic commands to use and document them here.

Step 1:  Create a central and local repository

First of all, I researched various online version-control systems, and chose bitbucket. It is free regardless of whether the code is open-source or not. Five users are included in the free version. Perfect for me!

Once I created a new repository, bitbucket provides basic commands for code checkout and checkin. The repository is at<username>/<repository_name&gt;.

To make a fresh checkout of this repository on the local filesystem, we use this command:
hg clone https://<username><username>/<repository_name&gt;

The local copy will have a .hg directory. To be able to push and pull changes more conveniently in the future, we modify the hgrc file inside the .hg directory. Add the path to the repository and the username, as follows:

default = https://<username>:<password><username>/<repository_name>
username = <username>

You can opt to not save the password in the file.

That’s it for a start! Now I can add new files to this directory.

Step 2: Adding new files

To check for the status of your added files, run hg st (short for hg status). New files will have a question mark symbol next to the filename. Before you can checkin new files to the local repository, you need to tell Mercurial that these files are part of the repository. This is done with the command hg add. For example:

> hg st
? index.html
? index.css
? index.js

> hg add .

> hg st
A index.html
A index.css
A index.js

Step 3: Committing / checking in files to the local repository

The command is simply hg commit.

The commit command will open the default text editor on the console for you to add comments.
Now, if you run hg st again, it will show nothing. Run hg log and you will see your last commit(s).

Step 4: Push the local commits to the central repository

You can make as many local commits as you want before pushing them to the central repo, but I would suggest you to push as soon as you can. Integrate often. Break sooner than later. Continuous integration — that’s what we call it in an XP development.

First we need to fetch other people’s commits to our local repo. After our local repo is updated, we can commit it to the central repo. The following two commands do it:

> hg pull
> hg push

That’s it for the basic usages of Mercurial!

No comments yet

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

%d bloggers like this: