Joe Yates' Blog

Programming and DevOps

Dotfiles

This post presents a “Pure Git” approach to managing dotfiles:

  • No symlinks,
  • No scripts,
  • Just Git configuration.

It is inspired by a comment on Hacker News.

How it works

The dotfiles repo is cloned to ~/.dotfiles, but Git is told that the working directory is the home directory.

From then on, it’s just a Git repo.

You can add any file in your home directory or subdirectories below it.

Getting Started

You could clone someone else’s dotfiles, but I advise against it - it’s best to understand what you have by stealing here and there.

Create your Dotfiles Repo

  • Create an empty Git repo somewhere (I use GitLab),
  • Follow the next section.

It is a good idea to add a README file to the root of your repo with a copy the setup steps below. That way, you know where to look every time you need to set up on a fresh computer.

Install your Repo

If you’re installing on a new computer, you can do the following:

1
2
3
4
5
6
$ git clone --bare <my repo> ~/.dotfiles
$ cd ~/.dotfiles
$ git config --local core.bare false
$ git config --local core.worktree <my home>
$ git config --local status.showUntrackedFiles no
$ git reset

These steps have cloned and set up the Git repo, but so far nothing has changed.

Now, run the following to check what will change when you “install” the dotfiles:

1
$ git status

Check you are not going to overwrite any local files with local changes you want to keep.

Beware - the next line installs the current version of files in your dotfiles repo, potentially overwriting different versions in your home directory (or subdirectories).

1
$ git reset --hard HEAD --

That’s it, your home directory is now managed by Git!

Maintenance

cd to your ~/.dotfiles directory.

Run git status to see what has changed.

Note that all file operations have a .. prefix

1
$ git add ../foobar

Outside the .dotfiles directory

Use the following to manage your repo without having to cd to ~/.dotfiles:

1
git --git-dir=$HOME/.dotfiles --work-tree=$HOME <command> [ARGS]

You can add this to your .bashrc or .zshrc as follows:

1
alias dotgit="git --git-dir=$HOME/.dotfiles --work-tree=$HOME"

If you add that to your dotfiles repo, that’s really all the configuration you need.

Notes

.gitignore

The is no .gitignore in this system, as Git is instructed to ignore all files that are not part of the repo.

Automation

If you want to automate adding local changes to your repo, it should be quite easy. Though, you’ll need to decide how to deal with remote changes if you are using your dotfiles on more than one computer.

Branches

If you have big differences between your needs on your various computers, you can even keep differences in branches!

That’s It

Enjoy your lightweight dotfiles!