Manage dotfiles with bare git repo

Based on a Hacker News solution proposed by StreakyCobra

Start from scratch

git init a bare repository.

git init --bare $HOME/.dotfiles

Configure an alias to set the git directory and working tree in .zshrc or .bashrc.

alias dotfiles='/usr/bin/git --git-dir=$HOME/.dotfiles/ --work-tree=$HOME'

Hide untracked files from git when calling dotfiles status.

dotfiles config status.showUntrackedFiles no

Usage

Call any git command with the configured alias.

dotfiles add <file>
dotfiles status
dotfiles commit
dotfiles remote add origin <remote>
dotfiles push origin main

Replicate the environment

Clone the remote repository.

mkdir .dotfiles
git clone --bare https://github.com/USERNAME/dotfiles.git $HOME/.dotfiles

Configure an alias to set the git directory and working tree in .zshrc or .bashrc.

alias dotfiles='/usr/bin/git --git-dir=$HOME/.dotfiles/ --work-tree=$HOME'

Hide untracked files from git when calling dotfiles status.

dotfiles config status.showUntrackedFiles no

Checkout.

dotfiles checkout

Gotchas

  1. If you have existing configuration files in the $HOME directory, checkout will throw an error. You can either (1) delete them or (2) back them up somewhere else.
  2. If you are cloning a repo containing Git submodules, you will need to run dotfiles submodule update --init regardless of whether you included the --recurse-submodules option when cloning.