Managing dotfiles in `git`
(2024-07-08)
[note]
Dealing with dotfiles can be a nuisance. I use a method that I read about in this Git tutorial (which is in turn based on a Hacker News post).
It is the most elegant approach I've come across. It does not need to clutter your home directory, it does not cause your shell prompt to show Git status info whenever you're in a subdirectory of $HOME. It stays out of the way, but is still instantly accessible whenever you need it and allows you to use the full power of Git.
Check out the linked tutorial for the full rundown. Here's the core idea, with my personal improvements added on top.
- Use
git init --bareto initialize a baredotfilesGit repository somewhere on your file system. I used$HOME/.dotfiles/. This repository will track all of your dotfiles while hiding away in a convenient location, which is not your home directory root. - Add a shell alias that uses this newly created bare dotfiles repository:
alias dot='/usr/bin/git --git-dir=$HOME/.dotfiles/ --work-tree=$HOME'
- Configure the
dotfilesrepo to not show untracked files in status output:
dot config --local status.showUntrackedFiles no
- Add some prints to your shell
rcfile in order to be reminded when you have forgotten to sync your stuff:
dotfiles_status="$(dot status)"
if ! echo "$dotfiles_status" | grep -q "nothing to commit"; then
echo 'There are dotfiles changes to commit. Run "dotfiles status" to see them.'
fi
if echo "$dotfiles_status" | grep -q "Your branch is ahead"; then
echo 'There are dotfiles commits to push. Run "dotfiles log" to see recent commits.'
fi
Jan Tuomi