dotfiles

Wednesday 17 February 2016 at 08:00 GMT

I talked about the ramifications of uploading your dotfiles to an online repository yesterday, but I also want to talk a little about the practice.

Assuming you're not running Windows (and if you are, I have no good solution except to stop right now), you probably have a bunch of files starting with "." in your home directory. Here's mine.

$ find ~ -maxdepth 1 \( -type f -o -type l \) -name '.*'
~/.babel.json
~/.bash_history
~/.bash_profile
~/.bashrc
~/.CFUserTextEncoding
~/.DS_Store
~/.gitconfig
~/.gitignore
~/.i3
~/.irb-history
~/.lesshst
~/.mkshrc
~/.NERDTreeBookmarks
~/.netrc
~/.node_repl_history
~/.nvim
~/.nvimrc
~/.profile
~/.racketrc
~/.scala_history
~/.sh_history
~/.sqlite_history
~/.tmux
~/.tmux.conf
~/.viminfo
~/.vimrc
~/.wget-hsts
~/.zcompdump
~/.zcompdump.zwc
~/.zhistory
~/.zlogin
~/.zlogout
~/.zprezto
~/.zpreztorc
~/.zprofile
~/.zshenv
~/.zshrc

Whoa, that's a lot. Let's see which ones I care about. For today, I just want to focus on one. How about .gitconfig, my global Git configuration, for now?

The first thing I'm going to do is create a new Git repository. This repository will be entirely selfish, so I recommend you name it as such. I called mine F••• You, Got Mine, because it makes me giggle every time I use it.

$ git init all-mah-stuff
$ cd all-mah-stuff

I always like to start new repositories with a README to get myself in the right mood.

$ echo '# All Mah Stuff. Hands Off. <3' > README.md
$ git add README.md
$ git commit -m 'README.'

Once you've created it, add a directory named dotfiles. You'll want to keep these in a subdirectory because at some point, you might find yourself adding files that need to be synchronised with your dotfiles but don't count as dotfiles themselves. For example, I have scripts in my bin directory which are used by my dotfiles, so it makes sense to version-control them together. (I had them split for a long time and it drove me nuts.)

Right. Copy your .gitconfig (or whichever file you're using) into your new dotfiles directory. I recommend dropping the "." at this time to make the repository easier to work with—it's tricky to manage lots of hidden files. Commit it with a snarky message.

$ mkdir dotfiles
$ cp ~/.gitconfig dotfiles/gitconfig
$ git commit -m "Here's a gitconfig. No, I don't know what it does either."

Next, we want to replace the .gitconfig file in our home directory with a symbolic link to our copied file.

$ ln -sf $PWD/dotfiles/gitconfig ~/.gitconfig

I expect we'll need to do that a lot, so let's make a script called init which does the job.

#!/bin/bash

DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"

ln -sf "$DIR/dotfiles/gitconfig" ~/.gitconfig

Wonderful. chmod +x init and commit.

The last thing to do is to push this repository somewhere else so we can use the same files on multiple computers. Go ahead and create a repository on GitHub, BitBucket or something else and push, push, push.

Now, whenever you make a change to that file, commit and push. Whenever you're on another computer, git pull and ./init before you get cracking. These two steps will make sure you always have your files synced, wherever you go.


If you enjoyed this post, you can subscribe to this blog using Atom.

Maybe you have something to say. You can email me or toot at me. I love feedback. I also love gigantic compliments, so please send those too.

Please feel free to share this on any and all good social networks.

This article is licensed under the Creative Commons Attribution 4.0 International Public License (CC-BY-4.0).