Learn Mercurial revision-control basics in 10 minutes
By: Poorshad Shaddel
Tags: Git, Mercurial, Programming, Software Development, Version Control

If you are using git as a version control tool, you can learn the basics of Mercurial as easy as pie. I was wondering if their motto(Work Easier Work Faster) is true or not and I determined to do basic version control actions using mercurial just by using --help and it worked like a charm!

Installation

Doesn't need any explanation: sudo apt install hg .

If you are using other operating systems this link may help you.

If everything goes well you can see the version using hg --version

Config mercurial

After the installation, we need to add a user (just like Git) using this command:

hg config --edit

And in the config file just fill username and you can start using Mercurial:

Initialize a repository

Use this command: hg init .

Now you are on the default branch which is similar to master in git.

If you want to be sure use this command: hg branch and this will show you: default

Create a file and commit that file

You can create your file inside the folder or with using this command in Linux or Mac OS: touch myFirstFile.js .

Now, this file is untracked. We can use the Add command to make mercurial track this file: hg add ./myFirstFile.js .

Create another file but don’t add that file. For example touch secondFile.js

similar to git status we can use hg status here and the result will be like this:

Result of hg status

Our first file which is added is showing with capital A(Which I assume it means Added!) and the second file(Which we created but didn’t add) is showing by a question mark.

To commit that file we must use: hg commit --message "my first commit message" . Now if you use: hg status again you can see that file is gone.

Question: How can I see my previous commits?

Answer: Exactly like what you do on Git

hg log will give you a brief history of your commits.

hg log result

changeset is something similar to a Git commit hash.

Create a branch and merge that to default

First check that you are in default branch: hg branch the result should be default .

Create a branch: hg branch branchName .

In Git you needed a -b option to create and checkout to that branch branch -b branchName but here you don’t need an option and after creating a branch your current branch is what you just created. Now the result of hg branch should be your branch name.

Switch branches:

Simply by using this command: hg update branchName .

Let’s create a file in this branch and commit that file.

touch thirdFile.js

hg add thirdFile.js

hg commit thirdFile.js

Now we want to merge our branch to default branch. First, we need to change our current branch: hg update default . Second, we need to merge our branch: hg merge -r myBranchName . Why -r ? -r means revision.

After the merge Mercurial shows a message:

Mercurial merge message

My branch name is dev . I had a file in my branch named thirdFile.js and after the merge command, it has a capital M at the beginning when I use hg status to see my working directory files (I assume that means Merge?!).

And the message says clearly that I have to commit this merge:

hg commit --message "merge branch"

Now the default branch is updated and it contains our thirdFile.js .

Clone a remote repository

Mercurial has a test repository and you can easily clone it by using this command:

Push and pull are mostly the same as git and I’m going to write a separate article about them.

And there you have it! Now you will hopefully feel more confident about using Mercurial! :)