Git bare repository

Backing up dotfiles using a git bare repository is a great way to manage, backup and version control them without cluttering $HOME directory. Here is a guide on how to set it up to work.

Initialise bare git repository

git init --bare $HOME/.dotfiles

Set alias for simplicity

alias config="/usr/bin/git --git-dir=$HOME/.dotfiles --work-tree=$HOME"

Store the permanent alias in a shell config file

Depending on your active shell, you should store the previous alias in one of the following configuration files: .bashrc, .zshrc, .profile. There should be the following line: alias config="/usr/bin/git --git-dir=$HOME/.dotfiles --work-tree=$HOME"

echo "alias config='/usr/bin/git --git-dir=$HOME/.dotfiles/ --work-tree=$HOME'" >> ~/.bashrc
source ~/.bashrc

Ensure untracked files don't interfere

Prevent the git from showing every untracked file in your home directory by calling the alias with another command.

config config --local status.showUntrackedFiles no

Start tracking dotfiles

Finaly add files or directories you want to track.

config add .bashrc
config add .vimrc
config add .config/nvim/init.vim

Commit the changes and push to remote

# commit changes to local git repository
config commit -m "initial commit"

# To back up your dotfiles to github or another git host:
config push -u origin master

Restore on a new machine

When restoring the files on a new machine you should clone the repository, set the alias

git clone --bare git@github.com:username/dotfiles.git $HOME/.dotfiles
alias config='/usr/bin/git --git-dir=$HOME/.dotfiles/ --work-tree=$HOME'
config checkout

If there are conflicts with existing files, resolve them or create a backup.

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