





















































(For more resources related to this topic, see here.)
Now that you have Mercurial set up on your computer, you are going to use it to track all the changes that can happen in a configuration directory, for example, in the configuration files of Apache2 (a web server). It could of course be used in any other type of directory and files: your home directory configuration files, some personal development projects, or some Mozilla Firefox configuration files such as user.js and prefs.js, and likewise. If you do not have Apache2 installed on your computer, you can do the same type of exercises, for instance, in $HOME/src. Also, with the owners of the /etc/apache2 files being root, you will need to change their owner to your account. For example, your username/group both being mary/mary:
$ sudo chown -R mary:mary /etc/apache2/
$ cd /etc/apache2/
$ hg init
$ hg status
? apache2.conf
? conf.d/charset
? […]
? sites-available/default-ssl
? sites-enabled/000-default
$ hg add apache2.conf sites-available/
$ hg status
A apache2.conf
A sites-available/default
A sites-available/default-ssl
? conf.d/charset
[…]
$ hg commit -m"initial version"
$ hg log
changeset: 0:7b3b5fcb16d0
tag: tip
user: Mary <[email protected]> […date…]
summary: initial version
$ vi apache2.conf
..make some changes: add "# new line" at the end of the file
$ hg status -m
M apache2.conf
$ hg diff
diff -r 7b3b5fcb16d0 apache2.conf
[…]
+# new line
$ hg commit -m"added a line at the end"
$ hg log
changeset: 1:02704fcf58b1
user: Mary <[email protected]>
summary: added a line at the end
changeset: 0:7b3b5fcb16d0 […]
$ hg diff
diff -r b0d2bfb95d81 apache2.conf […]
-# new line
+# new linez
$ hg revert apache2.conf
$ hg status
? apache2.conf.orig
$ tail -1 apache2.conf
# new line
$ hg remove sites-available/default-ssl
$ hg status
R sites-available/default-ssl
Instant Mercurial Distributed SCM Essential How-to
13
$ hg commit -m"removed unused ssl conf file"
$ ls sites-available/
default
$ hg update -C 0
$ ls sites-available/
default default-ssl
$ tail -1 apache2.conf
Include sites-enabled/
$ hg update -C
2 files updated, 0 files merged, 1 files removed, 0 files
unresolved
With this sequence, you already know how to manage versions of your personal projects: save a version, undo a change, retrieve an older version, and so on. Let's take a closer look at what happens under the hood.
After you type hg init, a directory called .hg is created:
$ ls -a
. apache2.conf envvars magic mods-enabled sites-availabl
.. conf.d .hg mods-available ports.conf sites-
This directory, called a repository, contains the history of your project and some Mercurial metadata. This is where Mercurial records all your revisions of tracked files (actually, it stores only the differences between each revision, like RCS/CVS/Subversion, and in compressed form, so the size is usually less than that of the the actual data!). And also, this is where it stores the commit messages, information about branches, tags, bookmarks, and so on. All the other files and directories beside .hg are your project files; they form what is called a working directory. If ever you would like to forget version control in this zone, you can just remove the .hg directory.
Another interesting thing to note is that, contrary to Subversion for instance, in a local .hg repository, you do not have less information than in a repository in a central server! Your installation of Mercurial actually allows you to have both a client command (or GUI), with which you can work locally or with distant servers, and a server, with which you can share your work with colleagues. This is because, with DVCS ( Distributed VCS ), there is no difference between the data checked out to work with and the data ready to be published. When you either create a local repository or clone an existing one from a server, you have all the necessary information (in the .hg directory) to become a publisher yourself.
This section will discuss supplementary commands or options.
First of all, Mercurial has many commands (the list of which you can get with the command hg help); in addition to adding and removing files from being version controlled, you can also use the copy command to copy a file into a new file, and similarly, you have a rename command. The difference between the OS file copy and the one that Mercurial has is that when you receive a change made by somebody else to the original file, it will also be applied by Mercurial to the new file; but for this magic to work, you have to tell Mercurial about the copies/renames so it can track them. There are also convenient commands, such as addremove, which allows you to automatically add all new files and remove (from version control) files that have been deleted.
Please refer to the built-in help system or to reference documentation, such as Mercurial: The Definitive Guide, available online at o http://hgbook.red-bean.com/, to get the complete list of options for each command. You have seen -m (or --message) in the commit command to specify a log message; if you omit this option, Mercurial will prompt you to get a message using $EDITOR. Also, you have seen -C (or --clean) in the update command; it will tell Mercurial to discard uncommitted changes when switching to another version (otherwise, local changes would automatically be merged into the requested version).
This article explained how to work with local files, either personal projects or files that you wanted to be version controlled (for example, source code or configuration files). You also learned how to create a new repository, make changes, and track them (selecting files to track, recording changes, and reversing those changes).
Further resources on this subject: