Managing your dotfiles

I was looking for some easy way to manage my dotfiles. Dotfiles are just configuration files most systems use today. They are stored in your home directory $HOME, and are prefixed with a . to make them hidden. An example of dotfiles: .gitconfig, .vimrc, .npmrc, .yarnrc

I stumbled upon a great post on Hacker News about how to manage your dotfiles. I followed those steps and decided to write a bit about my process.

Create a bare git repository

This will be the place where your dotfiles are version controlled. A bare repository is just a repository containing git objects and no tracked files. They are most often under .git in normal repositories, but bare is just that. When you create a repository that is just meant for storage – use bare.

git init --bare $HOME/.dotfiles

Create an alias to manage your dotfiles

echo "alias dot='/usr/bin/git --git-dir=$HOME/.dotfiles/ --work-tree=$HOME'" >> ~/.bashrc

This alias will create a dot command alias (you can name it whatever you’d like) that will call /usr/bin/git in a certain way and store it in ~/.bashrc file, so it will be loaded everytime you open Bash.

Now to explain those flags:

  • --git-dir=$HOME/.dotfiles/: No matter where you call the command, it will always point to this particular directory.
  • --work-tree=$HOME: It will always work with files in this particular directory.

So we made the storage of version controlled files point to $HOME/.dotfiles and the files it’s tracking will be in $HOME (because all dotfiles are located there).

Hide untracked files

Because we won’t be committing all files in $HOME we don’t want it to show untracked files. This is not necessary, it’s just so we don’t have all those untracked files visible when we run dot status

dot config --local status.showUntrackedFiles no

Version control your dotfiles

Now we would like to version control our dotfiles, so we can track changes, rollback to previous files or publish our files.

Get a list of untracked files

To get a list of files that aren’t being tracked, we have to add the --untracked-files=normal flag, because they are not visible by default because of the config status.showUntrackedFiles no we set earlier.

dot status --untracked-files=normal

Commit them

Now to commit our files, we just do it like regularly with git but instead we use the dot command.

dot add .gitconfig
dot commit -m "Add .gitconfig"
dot add .vimrc
dot commit -m "Add .vimrc"
dot add .bashrc
dot commit -m "Add .bashrc"

Add remote (e.g. GitHub)

Now if you want to publish your dotfiles, you can easily create a remote and push them to that. Like I did to my GitHub, because my dotfiles are publically available.

dot remote add origin
dot push

Leave a Reply

Your email address will not be published. Required fields are marked *