Typical Mercurial Usage

Unlike git, hg workflow maintains three trees in the local repository: Working Directory -> Index -> Head.

Create a New Repository

hg init

Checkout a Repository

hg clone /path/to/local/repository
hg clone username@host:/path/to/remote/repository

Propose Changes

hg add <directory | file>

This will add the changes to the Index.

Commit Changes

hg commit [-m message]

This will commit the changes to the Head, but not to the remote repository yet.

Push Changes

hg push [-b branch_name]

The changes in Head will be sent to the remote repository, whose default branch is master.

Branch Workflow

Create and Switch to a New Branch

hg branch <name_of_feature>
hg commit -m "Start <name_of_feature> branch."

Switch Back to Default

hg update default

Close the Branch

hg update <name_of_feature>
hg commit -m "Close <name_of_feature>." --close-branch

A branch is not available to others unless it is pushed to the desired remote repository.

Update and Merge Workflow

Examine Current Differences

hg diff -r <source_branch>:<target_branch>

Update Local Repository to the Latest Commit

hg pull

Merge Another Branch into Active Branch if Necessary

hg merge [branch]

After this step, one typically performs a commit and push.

Optional Tagging for Software Releases

hg tag -r <changeset_id> <semantic_versioning>

The version number should have semantic meaning.

Examine Repository History

hg log [-l limit_to_last_n_entries] [-u username]
hg glog [-l limit_to_last_n_entries] [-u username]

Miscellanea

Amending the Latest Changeset

hg commit --amend

Replace All Changes in Working Directory with the Latest Content in Head

hg update -C

Clear All and Start from the Server Commit

hg revert -r <changeset_id> <directory | file>

Generate Metadata and Diff for a Changeset All at Once

hg log --patch --rev tip

Generate Patch for a Commit

hg export -o /path/to/local/file -r <changeset_id>