18 February 2010

Versioning Dotfiles in Mercurial

I recently started using Mercurial to manage my UNIX configuration files. This is the first time I've felt comfortable tracking these in a VCS since back in the day when I kept my life in Subversion, and it's mostly thanks to the lead set by Gary Bernhardt. (Incidentally, you'll notice a lot of similarities between mine and Gary's dotfiles repository.) If you want to embark on a similar exercise, read on.

Getting started

  1. Initialize a Mercurial repository in your home directory.
    hg init
  2. Ignore whatever shows up when you run hg st. Note that ~/.hgignore defaults to regexp syntax. For more information, man 5 hgignore.
    for i in Documents Downloads .mozilla .ssh # etc.
      echo "^${i}\$" >> ~/.hgignore
  3. Once you're satisfied with the output of hg st, add your files and commit your initial import.
    hg add
    hg commit -m 'Initial import.'

Now you can create a repository on BitBucket, push to it, and keep track of changes remotely.

Cloning an existing repository on a new workstation

On the workstation you want to synchronize dotfiles on, do the following:

hg init
hg pull https://user@bitbucket.org/user/dotfiles-repo

That'll get the changes propagated. Now you just need to update the working copy (in Mercurial-land, pulling doesn't do this automatically). There are a few ways you can proceed. You can create another head with hg add and try to merge the two using hg merge, or you can obliterate the existing dotfiles in favor of the versioned versions with hg up -C, which destructively overwrites uncommitted changes.

That's the basic idea behind what I've been doing. Next time, I'll talk about how to manage workstation-local changes and divergent configurations. Meanwhile: do you use a VCS on your home directory? What are you using? How do you like it?


  1. Ha! Another friend of mine *just* wrote-up how to keep your dotfiles in Git: http://blog.nelhage.com/archives/134

  2. Nice! I think I found that as I was doing research for this post, actually.