Thoughts on data, statistics, computer science, and those other things people call life.

Menu

Setting Up SSH Keys to Work On Multiple Github Accounts

When I started working on my company, I set up a separate Github account from my personal account. I’d always had trouble pushing to the second account and resolved myself to adding my personal account to my work account each time I created a new repository. I’d tried following the various online blogs about how to set this up, but nothing had seemed successful. I finally took the time to try and figure it out. I slogged back through the dozen or so posts on setting this up and finally came across this Stack Overflow post with some comments, which made me realize something important.

First some basic steps:

This gets you a second set of keys and gets those keys added to your account. Then you need to configure git to use the right key for the right account. This is where the post was key. Every other post had these steps:

Then create the config file using whatever editor you want to use and add these lines (modified from this excellent post):

The key intuition here is that the Host is basically an alias. When you set the url for the remote (or clone the repository), you need to change the git hostname from this:

To this:

Notice the change of the host from just “github.com” to “github.com-user1”. This is what tells git to use the credentials specified in the config file. This was the piece I’d been missing. “Host” can be anything, but “Hostname” is the reference to the hostname in the original. In a sense, this passes through the config settings based on the specified host and picks up the correct identity file. I didn’t understand this, which is why it took me so long to get this working. Hopefully knowing this will help someone else trying to set this up for themselves. Happy hacking.

UPDATE: I just upgraded to macOS High Sierra and one of the lovely experiences was being prompted to enter my SSH password when I pushed code to Github. Knowing that my SSH credentials were in Keychain, I went to Google and found this lovely tidbit from this post:

That’s expected. We re-aligned our behavior with the mainstream OpenSSH in this area.