Simple but magical Dotfiles Management

Table of Contents

Introduction

Hey there! If you’re like me, you’ve got a bunch of dotfiles scattered across your system. These hidden gems hold the keys to our personalized software setup, but managing them can be a bit of a headache. That’s where Git comes in! Dotfiles are important because they allow us to personalize our software environment, making our daily tasks more efficient and enjoyable.

Why Git?

I’m not the first person to have this problem of course. So, I tried a few solutions like Chezmoi and Stow. But nothing hit quite like this article that I ran into years ago. It goes into the process that I continue to use to this day across different operating systems and without having to learn anything new I instantly know how it works (hint: because it’s just git.)

Git allows me to track changes in my dotfiles, keep them in their original locations, and easily sync them across different systems. All of this without the need for extra tooling or symbolic links. Plus, if you’re already familiar with Git, there’s nothing new to learn!

Setting Up Your Dotfiles Repository

I’m going to make a few assumptions here. First, you already have git installed. Second, you have created an empty GitHub repo called .dotfiles (feel free to change the name but then you need to update the commands below.) Alright, here’s how you can set up your own dotfiles repository with Git:

  1. Create a bare Git repository in your home directory. This will be used to track your dotfiles.
mkdir $HOME/.dotfiles
git init --bare $HOME/.dotfiles
  1. Create an alias for Git. This alias, which I like to call dot, will allow you to interact with your dotfiles repository. Now’s a good time to add it to your Shell config but I’ll cover that later too.
alias dot='$(which git) --git-dir=$HOME/.dotfiles/ --work-tree=$HOME'
  1. Hide untracked files. By default, Git shows all untracked files when you run git status. However, you probably have many files in your home directory that you don’t want to track. To hide these files, run the following command:
dot config --local status.showUntrackedFiles no
  1. Add the alias to your shell configuration file. To make the dot alias available in all shell sessions, add it to your .bashrc or .zshrc file.
echo "alias dot='$(which git) --git-dir=$HOME/.dotfiles/ --work-tree=$HOME'" >> $HOME/.zshrc

Useful Aliases

To make things even easier, I’ve set up a few more aliases for common Git commands. Feel free to use these or create your own, but you can add these to your .bashrc or .zshrc file.

alias dot='git --git-dir=$HOME/.dotfiles/ --work-tree=$HOME'
alias dcam='dot commit -a -m'
alias dst='dot status'
alias dl='dot pull'
alias dp='dot push'
alias da='dot add'
alias dotrm='dot rm --cached'

Each of these aliases is a shortcut for a common Git command. For example, dcam is a shortcut for dot commit -a -m, which commits all changes with a message.

Using Your Dotfiles Repository

With your dotfiles repository set up, you can now use the dot alias just like you would use git. Here’s how you can add a file to your repository, commit the change, and push it to GitHub:

dot add .zshrc
dot commit -m 'adding my first file'
dot push

Or you can pull new changes you’ve made on other systems

dot fetch
dot diff
dot pull

All of the things you are used to with git is available as long as you use your new alias, dot.

Syncing Your Dotfiles Across Multiple Systems

To sync your dotfiles across multiple systems, simply clone your dotfiles repository on the other system. After cloning, you can add, commit, and push like normal. Here’s how you can do it:

git clone --separate-git-dir=$HOME/.dotfiles [email protected]:<YourGHUsername>/.dotfiles.git ~

If you got an error about existing files being present, then I usually solve this via rsync. I just clone the bare repo to a temp directory, move the files to the rightful place with rsync, and delete the temp directory.

git clone --separate-git-dir=$HOME/.dotfiles [email protected]:<YourGHUsername>/.dotfiles.git tmpdotfiles
rsync --recursive --verbose --exclude '.git' tmpdotfiles/ $HOME/
rm -r tmpdotfiles

Conclusion

Alright, that is the gist of it. Managing your dotfiles with Git is a simple and efficient solution. It allows you to keep your dotfiles in their original locations, track changes, and easily sync them across multiple systems. Plus, if you’re already familiar with Git, there’s nothing new to learn!

Feel free to check out my .dotfiles repository on GitHub for some ideas. I use an Ansible playbook to install my favorite applications and pull down my .dotfiles on new systems but that’s for another time. And if you have any questions or comments, don’t hesitate to leave them below!