There are countless ways to manage dotfiles in git. For me none of the existing methods were satisfactory, so I rolled my own… This one has the benefit of making your home directory (or whatever directory you choose) into a real git repository, isolated from other repositories by using a custom GIT_DIR
.
The basic idea is that we launch a custom shell with the GIT_DIR
set to a directory named <name>.git
and GIT_WORK_TREE
set to the directory containing the code we’re managing using git.
I’ve been using and refining this system for a while, so decided to extract the pattern to make it easier to share. You can get the code from git-dotfiles.bash.
Here’s an an extract from my .bash_profile
:
# Git Dotfiles
source "$HOME/funcs/git-dotfiles.bash"
git_dotfiles_configure_shell_hook() {
# Override 'ga' func to add only modified files by default.
ga() { if [[ -z "$*" ]]; then git ls-files -m | xargs git add; else git add "$@"; fi; }
}
git_dotfiles home "$HOME"
git_dotfiles system /
We first source the bash library, then define a hook to run inside the custom shell (optional), before calling git_dotfiles
twice, once for the home directory, and again for the “system” directory at root. (I use the second one for managing nixos system configuration.)