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!

Comments

Before commenting, please read the comment policy.

Avatars provided via Libravatar