23 Sep 2022, 00:00

Managing dotfiles in git

Share

How to manage dotfiles in a bare git-repo

How to set up:

Initialize an empty git repo in a folder in $HOME:

1
git init --bare $HOME/.cfg

Now we want to set up an alias for git to work on this repo:

1
alias config='/usr/bin/git --git-dir=$HOME/.cfg/ --work-tree=$HOME' 

We want to hide untraced files, so we explicitly choose which files to track:

1
config config --local status.showUntrackedFiles no

Make config persistent, I use zsh, so I add the alias to .zshrc

1
echo "alias config='/usr/bin/git --git-dir=$HOME/.cfg/ --work-tree=$HOME'" >> $HOME/.zshrc

now its time to add a few existing config files to the repo:

1
2
3
config add .zshrc
config add .vimrc
config commit

I created a github repo and pushed my config to github:

1
2
config remote add origin <github-repo-url>
config push

Now, time to check out my config on another machine. First, checkout the repo using the --bare option

1
git clone --bare <git-repo-url> $HOME/.cfg

Define the config-alias

1
alias config='/usr/bin/git --git-dir=$HOME/.cfg/ --work-tree=$HOME'

And last: Check out the repo

1
config checkout