A Centralized VCS For Config Files

I often want to use versioning for individual isolated files, or collections of files, that live somewhere in my directory tree. However, because management of many independent repositories is hard, I am reluctant to create and maintain a repository wherever the file lives if the directory itself is not under active development.

Some examples of files like this are dotfiles in my home directory, and other preference or configuration files throughout my directory tree.

Instructions

Here's a solution that was based on a suggestion by Casey Dahlin on Doug Warner's blog. Comments from Peter Manis, Benjamin Schmidt and Martin Geisler were also helpful here, here, and here.

The idea is to create and manage a single repository for these files that will be easy to manage. Files can be added manually. Regular commits will be made on an ongoing basis, and can even be automated.

Initialization

First, create the repo in your home directory

cd$HOME
hg init

Safety Precautions

Peter Manis points out that the hg purge command can remove all files in the working directory that are not added to the repo!! He advises to explicitly disable this command for the repo by adding the following to the project-level .hgrc file located in $HOME/.hg/hgrc

[extensions]hgext.purge=!

List, Add, Forget, Remove, Commit, & Status

You can list, add, forget, remove, and commit files to the repo with the following commands

The status command reports on all files in the working directory whether they have been added to the repo or not. This can take a long time.

hg status

The Default Repo

To access the centralized repo from directories other than your $HOME directory, set the default path in your user-level .hgrc file located in $HOME/.hgrc

[path]default=$HOME

The centralized repo is accessible only if the current working directory (PWD) is not itself the working directory of another repo.

Warning: A danger of using this preference is that you may end up using the centralized repo when you intended to use a local repo. For example, if you accidentally call hg from a non-versioned directory.

To identify which repo you are working on, just type

hg showconfig bundle.mainreporoot

An easier solution is to add the following to your user-level .hgrc file

This can be used to refine the .hgignore file in order to initialize the repo

Glossary

Current Working Directory
: PWD

Working Directory
: "To introduce a little terminology, the .hg directory is the “real” repository, and all of the files and directories that coexist with it are said to live in the working directory. An easy way to remember the distinction is that the repository contains the history of your project, while the working directory contains a snapshot of your project at a particular point in history." quote

Hg Init
: Create a fresh repo. Fails when an existing repo exists in the working directory.

Hg Manifest
: List files currently in repo

Hg Add
: Add a file to repo

Hg Forget
: Forget files previously added to repo, before committing

Hg Remove
: Remove files previously added to repo, after they have been committed