website/site/blog/2019-04-27-manage-dotfiles-with-git.md

59 lines
1.9 KiB
Markdown
Raw Normal View History

2022-12-27 22:04:45 +01:00
# 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
<pre>
git init --bare ${HOME}/.cfg
</pre>
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:
<pre>
git --git-dir=${HOME}/.cfg/ --work-tree=${HOME} add .vimrc
git --git-dir=${HOME}/.cfg/ --work-tree=${HOME} commit -m "my .vimrc"
</pre>
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?
<pre>
alias config='git --git-dir=${HOME}/.cfg/ --work-tree=${HOME}'
</pre>
Put this in your `~/.bashrc` or `~/.kshrc` and you can now use the command
`config` in the same way you usually use git.
<pre>
config add .vimrc
config commit -m "my vimrc"
</pre>
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`.
<pre>
config config --local status.showUntrackedFiles no
</pre>
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...