How to manage your dotfiles with git

Based on a Hacker News solution proposed by StreakyCobra

In his own words, some advantages of this approach are:

No extra tooling, no symlinks, files are tracked on a version control system, you can use different branches for different computers, you can replicate you configuration easily on new installation.

Getting started

If you're starting from scratch, go ahead and…

  • create a .dotfiles folder, which we'll use to track your dotfiles
git init --bare $HOME/.dotfiles
  • create an alias dotfilesso you don't need to type it all over again
alias dotfiles='/usr/bin/git --git-dir=$HOME/.dotfiles/ --work-tree=$HOME'
  • set git status to hide untracked files
dotfiles config --local status.showUntrackedFiles no
  • add the alias to .bashrc (or .zshrc) so you can use it later
echo "alias dotfiles='/usr/bin/git --git-dir=$HOME/.dotfiles/ --work-tree=$HOME'" >> $HOME/.bashrc


Now you can use regular git commands such as:

dotfiles status
dotfiles add .vimrc
dotfiles commit -m "Add vimrc"
dotfiles add .bashrc
dotfiles commit -m "Add bashrc"
dotfiles push

Nice, right? Now if you're moving to a virgin system…

Setup environment in a new computer

Make sure to have git installed, then:

  • clone your github repository
git clone --bare $HOME/.dotfiles
  • define the alias in the current shell scope
alias dotfiles='/usr/bin/git --git-dir=$HOME/.dotfiles/ --work-tree=$HOME'
  • checkout the actual content from the git repository to your $HOME
dotfiles checkout

Note that if you already have some of the files you'll get an error message. You can either (1) delete them or (2) back them up somewhere else. It's up to you.

Awesome! You’re done.

Brazilian React & Rails full-stack developer. Living and working in Paris. Sharing a tiny piece of wisdom a week.

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store