CARVIEW |
Guides: Multiple GitHub Accounts
The solution detailed in this guide is fairly complex and may not be ideal for most users.
We recommend you check out the solutions detailed in Managing multiple clients and their repositories first.
The Problem
One might encounter a situation where two GitHub accounts might be necessary. Naturally, I mean two pay accounts in order to remain in good standing according to the TOS (or one free and one pay account). And we all want to stay in good standing with GitHub since they were awesome enough to make this great web application, right? Nevertheless, with that said, one might find one’s self in this situation.
A problem that one is likely to encounter in this situation is attempting to maintain two or more repositories that use more than one GitHub account. Using the default public/private key setup that GitHub suggests works great when only one account is involved, but when one attempts to clone a repository using a second account, by default, git attempts to use the first account, which is likely declined resulting in failure.
This article is assuming that you already have two git accounts created and that you already have the first key pair in place on both your local system and on GitHub.
Generating a Second Key Pair
Read this section COMPLETELY before actually doing anything. You could easily get yourself into a lot of trouble if you do not. GitHub identifies which account you are attempting to access by the SSH key that you give it. To this end, you cannot use the same key pair for more than one GitHub account. You will need to follow the instructions given in the guide entitled Providing your SSH Key. I think it would be unwise to reproduce part of that guide here.
Upon executing the ssh-keygen command, you will be presented with a prompt similar to the following:
Enter file in which to save the key (/Users/tom/.ssh/id_rsa):
WARNING: If you already had a key pair and are using it for anything at all, do NOT leave this blank nor should you use the default file suggested. It will overwrite your primary key pair, and that would be very bad.
Instead of using “id_rsa”, you should use something like “id_rsa_github_useraccount” to denote the purpose of the key. So ultimately, you would enter:
/Users/tom/.ssh/id_rsa_github_useraccount
Placing the Public and Private Keys
Private Key
You will not actually be putting the private key anywhere, but instead only referencing it. For this, you will need to open the SSH config (~/.ssh/config) file in your favorite editor. Once in this file, you should add the following:
# GitHub for cylence
Host github-tom
HostName github.com
User git
IdentityFile /Users/tom/.ssh/id_rsa_git_tom
The top line is a simple comment, you can leave this out of you choose. Next is the name of the Host, this is what you will use when referencing this connection and after the word “Host” followed by a space is completely arbitrary. The HostName must be “github.com” as is the same with the User.Notice that it is still “git” and not an account name. Lastly, the IdentityFile line must contain a non-reative path to your newly created private key (NOT ending with .pub).
Public Key
Once the private key is properly being referenced, you must tell GitHub what the public key is. You do this by accessing your account page on GitHub. Place your public key (including the carriage return at the end) into the “SSH Public Keys” textarea field.
Using the Newly Created Keys
From here on, everything is the same except for one component. To present an example, we will look at a standard clone command.
git clone git@github.com:account/repository.git
Traditionally, this command consists of “git clone”, the command to be performed, followed by a mostly ordinary ssh connection.
user@domain:path/somewhere/file.extension
What we will be changing is the domain portion of this line. Instead of using the domain “github.com”, we will be specifying the pre-configured SSH connection we built earlier. It is referenced using the Host line. Using our previous example, the end result would look like this:
git clone git@github-tom:account/repository.git
Thanks to mojombo for originally assisting me with this issue.