Tips and Tricks

(see also [:FAQ], [:HOWTOs], [wiki:CategoryTipsAndTricks CategoryTipsAndTricks])

TableOfContents

Include(/Index)

1. Undo an "hg add"

hg revert # take out of source control
hg rm -f # remove it

On Unix, to revert just the pending adds, you can use

hg status -an0 | xargs -r0 hg revert

2. Save a push URL so that you don't need to enter it each time

It is possible to store a default [:Push:push] URL that will be used when you type just "hg push". Edit [:.hgrc:hgrc] and add something like:

[paths]
default-push = ssh://hg@example.com/path

3. Track changes to a repository with RSS

You can track changes to projects and individual files with RSS feeds from hgweb. Here are some examples:

If you want to create web links to [:Tag:tagged] or [:Tip:tip] versions of a [:Repository:repository] or a file, you can do so like this:

5. Configuring Mercurial

See in [:.hgrc].

6. Abbreviate command options

It is possible to abbreviate command options:

hg revert --no-b
hg revert --no-backup

7. Ignore files from Emacs/XEmacs

Add the following to [:.hgignore]:

syntax: glob
*~

syntax: regexp
(.*/)?\#[^/]*\#$

8. Make a clean copy of a source tree, like CVS export

hg clone source export
rm -rf export/.hg

or using the archive command

cd source
hg archive ../export

The same thing, but for a [:Tag:tagged] release:

hg clone --noupdate source export-tagged
cd export-tagged
hg update mytag
rm -rf .hg

or using the archive command

cd source
hg archive -r mytag ../export-tagged

9. One liner to remove unknown files with a pattern

To make these work, replace the ls -l with the command you wish to execute (ie. rm). You can also tweak the parameters passed to hg status to filter by something other than unknown files (see hg help status).

hg status -nu0 | grep -z pattern | xargs -0r ls -l

The above command requires a current version of GNU grep. If you don't have one, you can use the following:

hg status -nu | grep pattern | tr '\n' '\0' | xargs -0r ls -l

10. Generating color diff output with extdiff and colordiff

You can use the [:ExtdiffExtension:extdiff extension] to get colorized diff output. If you've enabled the extension and have colordiff installed, the following [:.hgrc:hgrc] snippet will create a new cdiff command:

[defaults]
# suppress noisy extdiff header message
cdiff = -q

[extdiff]
cmd.cdiff = colordiff
opts.cdiff = -uprN

11. Using config substitution and the [DEFAULT] section of hgrc

The hgrc manpage gives a passing description of the [DEFAULT] section header but gives no notion of how one might use this (or the possible caveats). Here's an example:

Here's an example of the usage:

12. Using FileMerge.app/opendiff as the diff program (OS X)

The Developer Tools for OS X provide the excellent graphical diff program "FileMerge.app". The provided command-line wrapper "opendiff" for "FileMerge.app" will not work with ExtdiffExtension. Instead, use the script [http://ssel.vub.ac.be/ssel/internal:fmdiff fmdiff] which wraps "FileMerge.app" so that it responds like the usual diff program. Once fmdiff is in your path, just add the below to your .hgrc file

[extensions]
hgext.extdiff =

[extdiff]
cmd.opendiff =  fmdiff

and use

$ hg opendiff ...

13. hg diff does not support -foo option like gnu diff does

I use the following bash function to put the diff options I like most

hgdi ()
{
  for i in `hg status -marn "$@"`
  do
    diff -ubwd <(hg cat "$i") "$i"
  done
}

You can also use the extdiff extension to call GNU diff from Mercurial.

14. Handling binary files

as stated in BinaryFiles, you need to have a tool which manages binary merge. Joachim Eibl's new kdiff3 version ships a version qt4 version (on windows called "kdiff3-QT4.exe") which recognizes binary files. Pressing "cancel" and "do not save" leaves you with the version of the file you have currently in the filesystem. See also on CvsConcepts.

15. Diagnose "abort: Error" messages

I get a cryptic "abort: Error" message while pushing to my server. This is not enough info to figure out the problem. I tried hg -v --debug push but I still don't get anything more informative. What can I do?

16. Removing the working directory of a repository

If you forgot to specify -U on "hg [:Clone:clone]", doing

hg update null

will remove everything from the [:WorkingDirectory:working directory] of the [:Repository:repository]. See also [:Update:update]. ([http://selenic.com/pipermail/mercurial/2008-March/018332.html reference])

17. Setting the default context for diff to something larger

hg diff outputs 3 lines of context per default (see "hg help diff"). To change the default to for example 8 lines, add

[defaults]
diff = --unified 8

to the defaults section of your [:.hgrc]. However, this only affects the diff command itself. ([http://www.selenic.com/mercurial/bts/issue1076 reference])

18. Find repositories with GNU find

Users with access to GNU find may find these one-liners useful for managing all their repositories at once. They can of course be added to shell scripts to do more interesting things.

Print a list of directories which have repositories (a directory called ".hg" exists):

find ~/ -name ".hg" -type d -execdir pwd \;

Print a list of tracked files too:

find ~/ -name ".hg" -type d -printf "\t" -execdir pwd \; -execdir hg status -c -m -a -d \; -printf "\n"

19. Change temporary directory used on remote when pushing

See description of a [:Hook#tmpdirhook:hook for changing tmp directory] on remote when pushing.