.dotfile management

Posted on September 13, 2023 • 3 min read • 631 words
Share via

Manage those dotfiles in a more convenient way.

.dotfile management

How to manage dotfiles on multiple computers

Dotfiles and hidden files in a unix system is where your userbased configuration resides, managing them can be problematic and error prone if not done right.

I used to manage this by tracking all my files in git and cloning the repository down into a folder like ~/.dotfiles and then making symbolic links or simply copying the files to the correct location.

This has multiple pitfalls, not everything follows symbolic links by default. If you copy the files to the desired location then keeping tracks of changes can become hard and you need to copy each file back into the git repository to commit the new changes.

Sadly it isn’t possible to just manage every file in the correct location by a git repository in your homefolder as you’d then have issues with other git repositories being inside this repository.

But there is way…

This is the way!

We can actually manage every dotfile in their correct location directly in git. To accomplish this we need to move the .git repository to somewhere else than the base of our homefolder.

To do this run the following commands:

git init --bare $HOME/.cfg
alias config='/usr/bin/git --git-dir=$HOME/.cfg/ --work-tree=$HOME'
config config --local status.showUntrackedFiles no
echo "alias config='/usr/bin/git --git-dir=$HOME/.cfg/ --work-tree=$HOME'" >> $HOME/.zshrc

What we are doing here is initializing a bare repository in ~/.cfg and then create the alias config for git with commandline options specifying that the work-tree of this repository is actually our homefolder.

We then use our new alias to configure git so it by default does not show untracked files, as having every file in our homefolder show up when running git status (config status) is overwhelming. At last we append the alias to our .zshrc so the alias is persistent when we open a new shell.

Tracking our .dotfiles is now as simple as replacing git with config. And you can do that from anywhere in your homfolder and subfolders and by extension track any config files.

I track my .dotfiles in github and use both tmux and noevim (yes, I switched editor once again), this means i have files in ~/.zshrc, ~/.config/nvim and ~/.config/tmux.. all tracked inplace by the same git repository. To have a look at my setup checkout my GitHub

To use this newfound power…

Lets start versioning all the things. It’s as easy as:

config add .zshrc
config status
config commit -m "adding .zshrc config"
config push

And that is it, you have now added your first file. To manage files in subdirectories you can config add the path to the file or cd into the directory and config add from there.

The astute reader might have noticed that we never added a remote origin, and git will complain when we run config push. So to plase git we create a github repository, copy the repository url and run

config remote add origin <git-repo-url>

Now we can run config push again and see our files added in the repository.

On a new system or fresh install

Now the next time you need to setup your environment on a new system all you need to do is

alias config='/usr/bin/git --git-dir=$HOME/.cfg/ --work-tree=$HOME'
echo ".cfg" >> .gitignore
git clone --bare <git-repo-url> $HOME/.cfg
config checkout
# and now make the alias persistent
echo "alias config='/usr/bin/git --git-dir=$HOME/.cfg/ --work-tree=$HOME'" >> $HOME/.zshrc

Git might throw an error since ~/.zshrc might already be present, so just delete or move the files it complains about and do another config checkout.

That’s it you can now have a shared config on multiple computers and add changes to the config from anywhere. You can even do a seperate branches in the git repository for seperate hosts so you could do more specialized configurations for a host that you might not want replicated on all hosts.

Follow me

If for some reason you would want to see more of me I also generate web trash at theese locations. Should you wanna buy me a beer click the heart, as beer is love.