A clever way to manage your dotfiles with GIT2020-04-10

This method is inspired by an Article from Atlassian, which again is inspired by a thread on Hacker News.

I find this method very clever and elegant, and I just want to document my onw setup:

  1. Your dot files live in your home directory, as they always will - no links or other hacks needed
  2. The get versioned with git, and can be shared with other hosts using a central repo.

Initial setup (no repo created yet)

  1. Init a bare repo in your home folder:

    # Create a bare repo:
    $ git init --bare $HOME/.dotconfig
    # add an alias for the git command, using your home folder as work tree:
    $ alias dotconfig='/usr/local/bin/git --git-dir=$HOME/.dotconfig/ --work-tree=$HOME'
    # do not track un-added files
    $ dotconfig config --local status.showUntrackedFiles no
  2. Use the dotconfig command to manage your dotfiles:

    $ dotconfig status
    $ dotconfig add .zshrc
    $ dotconfig commit -m "Add zshrc"
    $ dotconfig add .config/nvim/init.vim
    $ dotconfig commit -m "Add neovim config"
    $ dotconfig push

Install to a new system / migrate to an existing one

  1. Make sure you set up the following steps BEFORE cloning the repo:

    $ alias dotconfig='/usr/local/bin/git --git-dir=$HOME/.dotconfig/ --work-tree=$HOME'
    $ echo ".dotconfig" >> .gitignore
  2. Clone the dotconfig repo:

    $ git clone --bare [your-repo-url] $HOME/.dotconfig
  3. Checkout the actual content from the bare repo:

    $ dotconfig config --local status.showUntrackedFiles no
    $ dotconfig checkout
  4. If there are existing dotfiles that conflict with the ones from your repo, you have to move them first.

With this technique managing your local dotfiles on multiple machines with version history become really simple.