Filtering Files

Not only does Mercurial give you a variety of ways to specify files, it lets you further winnow those files using filters. Commands that work with filenames accept two filtering options:

  • -I, or --include, lets you specify a pattern that filenames must match in order to be processed.

  • -X, or --exclude, gives you a way to avoid processing files if they match this pattern.

You can provide multiple -I and -X options on the command line, and intermix them as you please. Mercurial interprets the patterns you provide using glob syntax by default (but you can use regexps if you need to).

You can read a -I filter as process only the files that match this filter.

$ hg status -I '*.in'
? MANIFEST.in

The -X filter is best read as process only the files that don’t match this pattern.

$ hg status -X '**.py' src
? src/watcher/_watcher.c
? src/xyzzy.txt

Get Mercurial: The Definitive Guide now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.