website/site/blog/2019-04-27-manage-dotfiles-with-git.md
2022-12-27 22:04:45 +01:00

1.9 KiB

Manage dotfiles with git

I'm managing my dotfiles with git. My method serves me well for a few years already and so I think it's time to write it down.

If you think git, you might think of a dotfile repository and dozens of symlinks into the home directory. This is precisely what kept me from using git until I discovered bare repositories.

Create your dotfile repository with the --bare parameter

git init --bare ${HOME}/.cfg

This creates only a folder for git control files, which normally reside inside the .git folder within the repository.

You can now tell git to use ${HOME} as your work-tree directory. This makes git handle your home directory like all the files would be within the git repository. Now you can:

git --git-dir=${HOME}/.cfg/ --work-tree=${HOME} add .vimrc
git --git-dir=${HOME}/.cfg/ --work-tree=${HOME} commit -m "my .vimrc"

If course it is silly to type out such a long command every time you want to interract with your dotfiles. So why not create an alias?

alias config='git --git-dir=${HOME}/.cfg/ --work-tree=${HOME}'

Put this in your ~/.bashrc or ~/.kshrc and you can now use the command config in the same way you usually use git.

config add .vimrc
config commit -m "my vimrc"

Maybe you have been brave and typed config status already. This will list the content of your whole home directory as "untracked files". This is not what we want. We can run git config and tell it to stop doing this. But of course we must run our git, which is called config.

config config --local status.showUntrackedFiles no

Now git status will only check what's being tracked. So if you add your vimrc file and later change it, config status will show it, config diff will diff it...

You can now use the power of git with your new config command.

The solution is not perfect, but it comes pretty close...