Deric Pang

Managing Dotfiles With Git and Make

Last updated on

These steps will allow us to:

  • Track changes to dotfiles.
  • Backup dotfiles.
  • Easily install dotfiles on a new machine.

Initial Setup

Let's start by tracking .bashrc.

Set up a git repository for the dotfiles:

$ mkdir ~/dotfiles
$ cd ~/dotfiles && git init

Move .bashrc to ~/dotfiles/. Make sure to omit the . so it's not hidden in the repo:

$ mv ~/.bashrc ~/dotfiles/bashrc

Create a Makefile.

$ touch ~/dotfiles/Makefile

Put this inside ~/dotfiles/Makefile:1

pwd := $(shell pwd -LP)
.PHONY: bash
all: bash
bash:
        @ln -nfs "${pwd}/bashrc" ~/.bashrc

Install the dotfiles:

$ make all

After creating a new repo on GitHub, push the dotfiles:

$ git add .
$ git commit -m"Tracking my dotfiles."
$ git remote add origin <remote repository url>
$ git push -u origin master

Now, all changes to the dotfiles are tracked, and installing the dotfiles on a new machine just requires cloning the dotfiles repo and running make all.

Adding More Dotfiles

Let's add a Git config to the dotfiles:

Move .gitconfig to ~/dotfiles/. Again, make sure to omit the . so it's not hidden:

$ mv ~/.gitconfig ~/dotfiles/gitconfig

Update ~/dotfiles/Makefile to be:

pwd := $(shell pwd -LP)
.PHONY: bash git
all: bash git
bash:
        @ln -nfs "${pwd}/bashrc" ~/.bashrc
git:
        @ln -nfs "${pwd}/gitconfig" ~/.gitconfig

Install the dotfiles:

$ make all

Installing Dotfiles on a New Machine

It's as easy as:

$ git clone <remote repository url> ~/dotfiles
$ cd ~/dotfiles && make all

Conclusion

Since we're using Make, we can have much more complex setup logic. For example, I have different installation paths for different operating systems and separate dedicated Makefiles for things like Vim. If you're curious, you can check out my dotfiles on GitHub.

Footnotes

  1. Makefile syntax is finicky. Check out makefiletutorial.com.