A Repository for Your Dotfiles

If you don’t keep track of your dotfiles with Git, you should. It’s easy to do, and it saves a lot of time when setting up a new computer. Plus, you can go back to past configurations quickly.

In case you don’t know what dotfiles are and yet somehow found this post, dotfiles are “hidden” files that don’t show up in a normal directory listing. Often, the term dotfiles refers to those files that are not only hidden, but also store configuration for various applications that you use on a daily basis. For example, ~/.vimrc is a file in the home directory that stores Vim settings.

Suppose you have two dotfiles that you care about:

$ ls -a ~
.
..
.bashrc
.vimrc

In reality, there would be many more files here. Also, configuration files don’t have to end in rc; they just all happen to in this example.

First, create a new directory called dotfiles and run git init inside it.

$ mkdir dotfiles
$ cd dotfiles
$ git init

Go back to the home directory, and move your dotfiles to dotfiles.

$ cd ..
$ mv .bashrc .vimrc dotfiles/

Since these files are no longer in the location where the programs expect them to be, the programs might not know where to look. We can fix this using a symlink, or symbolic link. This is like a “shortcut” to the file. The “shortcut” will be in the home directory, and it will point to the real file. You can edit either one, and git will pick up the modifications.

$ ln -s /home/user/dotfiles/.bashrc /home/user/
$ ln -s /home/user/dotfiles/.vimrc /home/user/

The ln command creates a link between files, and the -s option tells it to make a symbolic link rather than a hard link. I used absolute paths here for clarity, but relative paths work as well.

That’s it. This basic setup will help you store your configurations and keep track of changes to them. You can even create scripts to do the linking for you when you drop your files onto a fresh OS, but that’s left as an exercise for the reader.