Renaming Files

It’s rather more common to need to rename a file than to make a copy of it. The reason I discussed the hg copy command before talking about renaming files is that Mercurial treats a rename in essentially the same way as a copy. Therefore, knowing what Mercurial does when you copy a file tells you what to expect when you rename a file.

When you use the hg rename command, Mercurial makes a copy of each source file, then deletes it and marks the file as removed.

$ hg rename a b

The hg status command shows the newly copied file as added, and the copied-from file as removed.

$ hg status
A b
R a

As with the results of a hg copy, we must use the -C option to hg status to see that the added file is really being tracked by Mercurial as a copy of the original, now removed, file.

$ hg status -C
A b
  a
R a

As with hg remove and hg copy, you can tell Mercurial about a rename after the fact using the --after option. In most other respects, the behavior of the hg rename command, and the options it accepts, are similar to the hg copy command.

If you’re familiar with the Unix command line, you’ll be glad to know that the hg rename command can be invoked as hg mv.

Renaming Files and Merging Changes

Since Mercurial’s rename is implemented as copy-and-remove, the same propagation of changes happens when you merge after a rename as after a copy.

If I modify a file, and you rename it to a new name, and then we merge our respective changes, my modifications to the file under its original name will ...

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.