Simplest Way to Sync Dotfiles and Config Using Git
Oct 23, 2016
Most devs have two or more computers that they use. In my case it is my home Macbook and one in the office. We want to have the same config, keybinding or aliases across those machines.
There are several ways to do it. I tried using symlinks to a folder that is synced through git, but some apps won’t load symlinked files. For example, if you try to symlink
~/.zshrcthen zsh will load as if you don’t have an rc file.
After looking around for a while, I came upon a post in Hackernews about this issue. It led to this article by Atlassian. It is by far the best solution.
This technique is using
git init --barewhich I have never encountered before. You don’t really need to understand it to use it, but for reference sake, this is what
--bare Create a bare repository. If GIT_DIR environment is not set, it is set to the current working directory.
That article is very succint and you could just follow the steps laid out there. In summary, use these commands to set it up.
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/.bashrc
Replace the last
.bashrcto whatever shell that you are using e.g. in my case it is
All git operation are permitted, you just have to use
git. For example:
config status config add ~/.zshrc config commit -m "Add vimrc" config add ~/.xvimrc config commit -m "Add xvimrc"
To sync those files with another computer, you need to setup a repo. I recommend using Gitlab as they have free private repos. After creating new repo in Gitlab, you can do:
config remote add origin firstname.lastname@example.org:yourname/testrepo.git config push -u origin master
From this point on, you can treat it like normal git repo. One interesting use case would be to create different branches for different settings that you wanted to use.
Setting up new machines
Now that you have the first machine set up and syncing nicely, you can start setting up the second machine by first adding this line to .zshrc or .bashrc.
alias config='/usr/bin/git --git-dir=$HOME/.cfg/ --work-tree=$HOME'
Then clone the repo from Gitlab to the folder:
git clone --bare email@example.com:yourname/testrepo.git $HOME/.cfg
From here, you can
checkoutto your hearts content.
You really should read the source article since it explains the steps in detail except adding remote repo.
Thanks for reading!