A better way to handle multi-account GitHub

I mentioned my crappy approach to using multiple GitHub accounts on a Slack I’m on, and someone else pointed out there’s a much easier approach: Instead of using wrapper scripts to set up different environments, you can fake it using .ssh/config and .gitconfig rules.

First, set up key rules with .ssh/config:

Host  github-work
  Hostname github.com
  IdentityFile ~/.ssh/id_work
Host github-personal
  Hostname github.com
  IdentityFile ~/.ssh/id_personal

Next, set your origin based on whether the workspace is work or personal; for example, git clone git@github-work:work-org/project.git (and of course you can git remote set-url origin for existing workspaces).

Finally, to handle the different author name and email, git 2.15 and later support conditional includes. If you keep all of your work projects in a separate directory, you can put this into your .gitconfig:

[includeIf "gitdir:~/wfh"]
path = ~/.gitconfig-work

then .gitconfig-work includes your work-specific configuration, e.g.:

[user]
name = Boring Legal Name
email = work-email@example.com

Thanks, Silas!

Using multiple GitHub accounts from a single macOS/Linux account

Let’s say you’re stuck working from home due to an ongoing apocalypse. Let’s say that you use separate GitHub accounts for personal projects vs. work (for one of any number of reasons), and that when you’re working from home you’re using a personal computer. Let’s say that for Reasons it’s not feasible for you to juggle multiple user accounts on said computer, and you need to be able to access both of your GitHub accounts without a lot of hassle.

The main problem is that GitHub ties your ssh key to your account (out of necessity), but all connections to GitHub are via the master git@github.com account, so there’s no easy way to differentiate which key to use at runtime.

So, here’s how I managed to set things up so that I could select a GitHub account on a per-shell basis.

Read more…