Tips for Mercurial (Hg)

This post will probably become a living document, i.e. will be updated sporadically with small tips, tricks and reminders on how to do certain tasks with Mercurial (Hg), because most of it will be short entries that won’t justify an article on its own.


The following command gets the revision history from the repository from when it was labeled with the tag “version_x.y.z” and now (tip). The output is in reverse chronical order and will be saved in file.txt.

hg log  -r "tip : tagged(version_x.y.z)"
        --template "{date|isodate} {desc}\n----------\n"
        > file.txt

The content of file.txt will look like this:

2015-05-16 02:00 +0200 Project maintenance work

- Added license file
- Updated links to homepage and copyright notice
- Revised ReadMe.rst
2015-05-16 00:45 +0200 Changed the command that gets the changeset ID 

Instead of {isodate} one could also use {rfc3339date}, which will render the timestamp conforming with the ISO format: 2015-05-16T02:05:07+02:00.
Or {date|shortdate}, which will just get year, month and day in the YYYY-MM-DD format.

To print the ID of the revision, use {node} or {node|short}.

The tag(s) can be read via {tags}.

For the author: {author} (full name and e-mail address) or {author|user} (just the username).

To shorten the description to a one line summary, use {desc|strip|firstline}.
Or to print a multiple-line commit message on one line, use {join(splitlines(desc), ‘ ‘)}.

More about Template Usage can be found at the Hg help site.