Dotfiles saving strategy with git

05 de septiembre 2022Comentariosherramientas desarrolloDavid Poza SuárezComentarios

For a long time, I've been thinking about the fact I have this blog totally abandoned. I'm not finding the motivation in order to write down stuff about the progress of my projects, useful information for devs, Linux, automation or whatever thing present on my daily basis.

And at the same time, I'm struggling on devoting time to learning English. I continue with FCE preparation of course, but at my own pace. - If slow but persistent, that's good"... But maybe there's a possibility of combining both topics...

What if I start to write in english?, that would be the perfect excuse for writing entries, even if it's nothing special o newish, it meets the purpose of being helpful for my english. So let's get down to the business!

I'm in love, I cannot hide it, I'm in love with git :D Some time ago I discovered the "technique" of using a bare git repo as dotfile container, so that we can easily replicate our config among different machines, keep different ones and track their changes.

Backing up

First of all, a git bare repository is a repo without the working directory mounted on the filesystem. The idea is to keep the actual repo separated from the working directory and having this one pointing to the home folder.

For this example we'll be using .config as the path for the bare repo.

git init --bare $HOME/.config

The new location of the working can be specified using git command as follows:

git --git-dir=$HOME/.config/ --work-tree=$HOME

NOTE: Since the working directory is containig the bare repo we have to prevent this one to be included in commits, so add that (.config) to the .gitignore.

Another important consideration is the fact that not every file in our home should be tracked, only chosen files must be uploaded, and we don't want the rest to be reported during status command. So, we are defining the next config:

git --local status.showUntrackedFiles no

Of course, having to type always the --git-dir and --work-tree parameters is anoying so we can create a shell alias (put it in your .bashrc or .zshrc):

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

As I pointed before we don't want to add . everything in our home, so we have to add those files one by one. But if we are only modifying exisiting files, we can use:

gitdotfiles add -u // this will add every single modified/deleted file at once

Restoring

The final scenario is when you have to restore those dotfiles on a new machine, that can be done as (remember also to restore the alias):

git clone --bare <url> $HOME/.config
gitdotfiles checkout -f // we do force the override of existing files
gitdotfiles config --local status.showUntrackedFiles no

I'm quiting for now. Bye, see you