Syncing Dotfiles

For the longest time, I didn’t keep my workspace config in a shared place. I have multiple devices that work on bot at home and for my job. Redoing the config has always been a bit of a pain, so I leave things as default as possilble. Until recently, I didn’t really have a strategy that I was happy about for managing a cli config. Talking to a friend about their customized environment lead me to search for something to accomplish the goal that I would be happy about.

I ran across the following link from the atlassian git tutorials site actually.

It suggests creating a bare repository at $HOME that you checkout into the environment to handle any file you want to manage. This was a revelation to me, because the biggest issue for me was having to move files from a dotfiles repo into the actual environment. I wanted a way to checkout files and updates into the environment in-place.

Once you have the repo setup, pulling and pushing updates is as easy as

config pull
touch .my_dotfile
config add .my_dotfile
config commit -m 'touched my dotfile'
config push

The standard git flow, but everything is handled with a custom alias and files live in-place. All the git niceties are included like; checking diffs, resolving merges, change logs, accessible on nearly any os and no running daemon or fs hooks.

Bootstrapping

There is a bit of setup to get the config repo and scripts started. I could probably make this a snippet to pipe into a shell from curl.

Add a temporary alias in the current shell

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

add a pre-requisite .gitignore file to prevent wierd recursion

echo ".cfg" >> .gitignore

Then clone the repository

git clone  --bare git@git.sr.ht:~auxreturn/cfg $HOME/.cfg

Run checkout to pull the repo files into the working tree

config checkout

Remove or merge any conflicts that show up from the previous step by taking backups.

mkdir -p .config-backup && \
config checkout 2>&1 | grep "\s+\." | awk {'print $1'} | \
xargs -I{} mv {} .config-backup/{}

Important! Set the showUntrackedFiles git config variable to “off”. Otherwise, “config status” will walk the whole user directory for changes, we only care about manually tracked files.

config config --local status.showUntrackedFiles no

last-modified: 2024-03-17 21:37 CDT