Mercurial Basics

Basic Mercurial Commands

Mercurial (just like Git) is a distributed revision control system; Distributed revision control takes a peer-to-peer approach to version control, as opposed to the client-server approach of centralized systems. Rather than a single, central repository on which clients synchronize, each peer’s working copy of the codebase is a complete repository

Looking for a lot more detail? This guy does a great job at explaining Mercurial and distributed revision control: http://betterexplained.com/articles/intro-to-distributed-version-control-illustrated/

Quick Start

Clone a project and push changes

$ hg clone http://selenic.com/repo/hello
$ cd hello
$ (edit files)
$ hg add (new files)
$ hg commit -m 'My changes'
$ hg push

 Create a project and commit

$ hg init (project-directory)
$ cd (project-directory)
$ (add some files)
$ hg add
$ hg commit -m 'Initial commit'

Explanation

Initialize Hg Project

# Cloning a new project
$ hg clone <Remote Path for repo> <Local Path for repo>
eg. hg clone http://selenic.com/hg/ .

Review Code Before Committing

$ hg status         # show all non-ignored files
$ hg diff -s        # all changes your about to commit
$ hg revert y2      # Restores the local copy to the default tip state
$ hg log            # change set

Commit and Push code to server

$ hg pull -u        # get latest changes and update repo
$ hg add            # add 'unknown' files 
$ hg commit         # commit all changes into new changeset, edit changelog entry 
$ hg push           # send changeset to server

Working with branches in Mercurial

Branches

The default branch is earmarked as our release branch. Code that is in default can and will be deployed at any time, with short notice.

All work apart from the very trivial should be done in named branches.

Branches should be propagated to all associated project subrepositories, (this needs to be done manually).
The reason for the branch propagation is to prevent any change from spreading into mainline releases accidentally.

Some branch use-cases are below:

Developer creates a new branch

hg branch new-feature
hg commit
hg push --new-branch

Developer creates a new branch and propagates branch to subrepos

cd $APP
hg branch new-feature
cd $APP/subrepos/commonLib
hg branch new-feature
hg commit
hg push --new-branch
cd ../
hg commit
hg push --new-branch

Developer switches to new branch

hg up new-feature

Developer does work on new branch

hg commit
hg push

Developer needs to do work on trunk (called “default” in mercurial)

hg up default
<<Make Changes>
hg commit
hg push
hg up new-feature

Developer needs to merge changes from default into project branch (ReBase)

hg up new-feature
hg merge default
hg commit
hg push

Developer completes work on a branch

hg up default
hg merge new-feature
hg commit
hg push

Developer abandons a branch (task cancelled)

hg up new-feature
hg commit --close-branch
hg up default