Managing Dotfiles with Git and Github

1 minute read

Looking for the perfect solution to managing your dotfiles without having to use symlinks? This configuration will allow you to easily migrate your dotfiles to every unix machine you touch while maintaining version control.

Never leave home without your dotfiles again!

Full credit for this solution goes to StreakyCobra, seliopou, and telotortium on the Y Combinator forums. Thank you, guys!

Warning! If caution isn't taken with the instructions below, you may accidently delete your dotfiles. I highly recommend backing up your dotfiles just in case.

Setting up your dotfiles

  1. Create an empty github repo for your dotfiles.
     https://github.com/YOURUSERNAME/dotfiles
    
  2. Clone the repo into a temporary directory.
     git clone --separate-git-dit=$HOME/.dotfiles https://github.com/YOURUSERNAME/dotfiles $HOME/dotfiles-tmp
    
  3. Copy the contents of that directory into your home folder.
     cp ~/dotfiles-tmp/. ~ -r
    
  4. Remove the ~/.git file
    rm ~/.git
    

    Warning! If you get warnings about a directory not being a git repo, or if your shell becomes very slow / zgen starts throwing errors, you probably forgot to delete this.

  5. Delete the temporary directory.
     rm ~/dotfiles-tmp -r
    
  6. The first time you setup these dotfiles, add the following dotfiles alias to your shell dotfile (.bashrc, .zshrc, etc), or wherever you put your aliases.
     alias dotfiles='`which git` --git-dir=$HOME/.dotfiles --work-tree=$HOME'
    
  7. Source your shell dotfile (.bashrc, .zshrc, etc) to make sure the dotfiles alias is now available to you.
     source ~/.zshrc
    
  8. To prevent $ dotfiles status from displaying every file in your home directory as being untracked, run:
     dotfiles config status.showUntrackedFiles no
    

Adding dotfiles to the git repo

  • Use dotfiles command the same way you would use the git command.
      dotfiles add ~/.zshrc ~/.tmux.conf
      dotfiles commit -m "Added .zshrc and .tmux.conf"
      dotfiles push
    

Pulling dotfiles to new machines

  • Simply do steps 2, 3, 4, and 5 from the initial setup instructions.
  • Or… you could make a script on another gitrepo or on your github pages that runs those steps for you.
      #!/bin/sh
      git clone "--separate-git-dir=$HOME/.dotfiles" https://github.com/$1/dotfiles $HOME/dotfiles-tmp
      cp ~/dotfiles-tmp/. ~ -r
      rm ~/.git
      rm ~/dotfiles-tmp -r
    

    To use my script:

      curl -lO https://techknowfile.dev/scripts/dotfiles.sh
      sudo chmod +x ./dotfiles.sh
      ./dotfiles.sh YOURGITHUBUSERNAME
    

Updated: