Mercurial Reference

Set up configuration (only needs to be done once per machine):

On Windows, create a file called %USERPROFILE%\Mercurial.ini (that's spelled $USERPROFILE/Mercurial.ini if you are in gitbash) containing the following lines:

[ui]
username = Your Name <you@some.domain>
editor = your_editor

[extensions]
color =

[color]
mode = win32

On OS/X or Linux, create a file called ~/.hgrc with the contents:

[ui]
username = Your Name <you@some.domain>
editor = your_editor

[extensions]
color =

Display help text for a Mercurial command:

hg help command_name

Initialize the current working directory as a repository:

hg init

Display the status of the repository:

hg status

Mark specific files to be tracked in the repository:

hg add filename_1 filename_2

Mark specific directories to be tracked in the repository:

hg add directory_1 directory_2

Mark all untracked files and directories to be tracked in the repository:

hg add

Stop tracking a specific file:

hg forget filename

Commit changes in all modified files to the repository's history: (Without -m and a message, this command runs a text editor.)

hg commit -m "Some message"

Commit changes in specific modified files to the repository's history: (Without -m and a message, this command runs a text editor.)

hg commit filename_1 filename_2 -m "Some message"

View the history of the repository:

hg log

Display differences between the current state of the repository and the last saved state:

hg diff

Display differences between the current state of a specific file and the last saved state:

hg diff path/to/file

Display differences between the current state of a specific file and its state in a specific earlier revision:

hg diff --rev 27 path/to/file

Display changes made to a specific file in a specific earlier revision:

hg diff --change 24 path/to/file

Erase changes to a specific file since the last commit (modified file is saved with a .orig suffix before reverting):

hg revert path/to/file

Erase changes to a specific file since the last commit without saving .orig backup:

hg revert --no-backup path/to/file

Erase all changes since the last commit:

hg revert --all

Restore file to its state in a previous revision:

hg revert --rev 16 path/to/file

To add the URL of a clone of a repository on Bitbucket, edit (or create) the .hg/hgrc in the repository to include:

[paths]
default = URL

To add the path of a local clone of a repository, edit (or create) the .hg/hgrc in the repository to include:

[paths]
nickname = path/to/repository

Display a repository's clone URLs and/or paths:

hg paths

Push changes from a local repository to a (default) Bitbucket clone of the repository:

hg push

Pull changes from a (default) Bitbucket clone of the repository:

hg pull

Pull changes from a local clone of the repository:

hg pull /path/to/repository

Pull changes from a local clone of the repository that you have added a nickname path for:

hg pull nickname

Update a repository with the changes pulled from a clone of the repository:

hg update

Merge changes pulled from another clone of the repository using the GUI kdiff3 to view and resolve conflicts:

hg merge --tool=kdiff3

Clone a repository from Bitbucket:

hg clone URL

Clone a repository from a local path:

hg clone path/to/repository

Note that when you clone a repository, either by a URL or by a path, the URL/path of the source repository is automatically added to the [paths] section of the new repository's .hg/hgrc file as the default path so that hg push and hg pull just work.