Running Commands Without Any Filenames

Mercurial’s commands that work with filenames have useful default behaviors when you invoke them without providing any filenames or patterns. What kind of behavior you should expect depends on what the command does. Here are a few rules of thumb you can use to predict what a command is likely to do if you don’t give it any names to work with.

  • Most commands will operate on the entire working directory. This is what the hg add command does, for example.

  • If the command has effects that are difficult or impossible to reverse, it will force you to explicitly provide at least one name or pattern (see below). This protects you from accidentally deleting files by running hg remove with no arguments, for example.

It’s easy to work around these default behaviors if they don’t suit you. If a command normally operates on the whole working directory, you can invoke it on just the current directory and its subdirectories by giving it the name ..

$ cd src
$ hg add -n
adding ../MANIFEST.in
adding ../examples/performant.py
adding ../setup.py
adding main.py
adding watcher/_watcher.c
adding watcher/watcher.py
adding xyzzy.txt
$ hg add -n .
adding main.py
adding watcher/_watcher.c
adding watcher/watcher.py
adding xyzzy.txt

Along the same lines, some commands normally print filenames relative to the root of the repository, even if you’re invoking them from a subdirectory. Such a command will print filenames relative to your subdirectory if you give it explicit names. ...

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.