What Are Dotfiles?

Dotfiles are those sneaky files in your home directory that start with a dot. Examples include .bashrc or .vimrc. Many users will never encounter these types of files because they generally don't show up in the file viewer. However, a quick ls -al in your home directory will reveal all the dotfiles you've created (or that have been created for you). As someone who talks to computers all day, these dotfiles are my bread and butter for keeping my working environment running smoothly and effortlessly.

More specifically, dotfiles are just text files that can store configurations and settings for the various programs used on the machine. For example, I like to use vim as my text editor sometimes, and I have it configured so that it looks and behaves a certain way every time I open it. There is a specific dotfile for vim in the home folder, called .vimrc, where I can store all of these configurations.

For me, the magic of dotfiles really shines through when they're stored in a public repository for all to see. First and foremost, I'm not just sharing these files with other people — I'm sharing them with my future self as well. So if I fry my motherboard with hot coffee or do a fresh install of Arch Linux for the millionth time, I don't have to lose all my beautiful vim configurations. I can just pull in the remote repository and get my working environment set up exactly the way it was. Secondly, sharing dotfiles is a great way for developers to communicate tips and tricks about the way they work every day without having to articulate it in plain English.

Keeping Dotfiles Under Version Control

First I'll talk about how to store dotfiles in a remote repo, so that you can have your dev environment up and running seconds flat. Regardless of whether you're already using dotfiles, the process of adding them to a version control system is fundamentally the same.

  1. Initialize a bare git repository ~/.cfg in order to track the files.
  2. git init --bare $HOME/.cfg
  3. Create an alias for the usual git command and call this alias config. This is a nice shortcut for when I want to do version control-ly things like push and pull on my new config files. Now I can type config pull instead of git pull.
  4. alias config='/usr/bin/git --git-dir=$HOME/.cfg/ --work-tree=$HOME'
  5. Set of a flag local to this repository to hide files that are not being tracked. This will clean up the output of config status (our new git status) so that only the files I have explicitly tracked show up.
  6. config config --local status.showUntrackedFiles no
  7. Add the above alias to my .bashrc file for future use (this step can also be done by manually editing the file)
  8. echo "alias config='/usr/bin/git --git-dir=$HOME/.cfg/ --work-tree=$HOME'" >> $HOME/.bashrc
  9. Next I'll configure the remote repository I'm going to use. Since I'm using GitHub, I just create a new repository and call it "dotfiles". Then, back in $HOME,
  10. config remote add origin git@github.com:my-username/my-new-repo-name.git
    Since it's my first time pushing to this repo, I might need to set the upstream while I do so:
    config push --set-upstream origin master
    Now any file in my $HOME directory can be added to version control by using normal git commands, but using the alias that we created for git called config instead. For example, the following will add, commit, and push your local .bashrc file to the remote Github repository:
    config status 
    config add .bashrc
    config commit -m 'Add bashrc'
    config push 
    

Now that all my dotfiles are in version control, any time I try out a new Linux distirbution or get a new computer, I can have my old environment at my fingertips in seconds. Try it out and let me know what you think!