IN-Decent

Re-decentralizing internet with free software

Manage and Sync Dotfiles With Git

Posted at — Jul 17, 2020

In Unix systems, most applications can be configured by users in addition to system configuration by placing configuration files for the application in user home directory. Often, these files begin with a dot character and will be hidden. These files are commonly referred to as dotfiles.

Managing and synchronising these dotfiles between multiple systems become more complex, especially when there is new content added now and them. By using git to manage them, it can be easily synchronised across machines, backup becomes easy, also change history will be available to track why a particular change was made, different branches can be used for specific machines and top of it all no additional tooling is necessary.

To start tracking your dotfiles with git

git init --bare $HOME/.dotfiles.git
alias dotgit='git --git-dir=$HOME/.dotfiles.git --work-tree=$HOME'

# Adding it to my bash shell's aliases file. Change the config file name according to your login shell.
echo "alias dotgit='git --git-dir=$HOME/.dotfiles.git --work-tree=$HOME'" >> $HOME/.bash_aliases
dotgit config --local status.showUntrackedFiles no
dotgit add .bashrc .bash_aliases .vimrc .gitconfig
dotgit commit -m "Added inital config files"

Distributing dotfiles repo without a central server

dotgit remote add backup file:///share_path/dotfiles.git

#now start pushing new changes to the backup central repo
dotgit push --set-upstream backup master
#using ssh
git clone --bare ssh://<host>:<port>/repo_path/dotfiles.git $HOME/.dotfiles.git

#using git file protocal from a share path
git clone --bare file:///mnt/share_path/dotfiles.git $HOME/.dotfiles.git
#set alias temporarily before checkout, as after checkout the alias we already added will be available next time.
alias dotgit='git --git-dir=$HOME/.dotfiles.git --work-tree=$HOME'

dotgit checkout

If the same files are already present, git will complain about modifying them. Just delete them or rename them to have the new files available.

Additionally, we have to configure git to ignore untracked files in this machine, since we added it as a local config and will not be available in a cloned repo.

We are done and we can make the changes from any system and push them to an internal central repo, without sending any data out or using additional tools.

References:

  1. Manage your dotfiles with Git
  2. Ask HN: What do you use to manage dotfiles?
  3. Pro Git Book - 4.1 Git on the Server - The Protocols