Say you are working for multiple clients and each requires a separate GitHub account, plus your personal account. You can automate logging into all of them.
Step 1: Create your first SSH key.
https://docs.github.com/en/github/authenticating-to-github/connecting-to-github-with-ssh/generating-a-new-ssh-key-and-adding-it-to-the-ssh-agent
In GitHub’s instructions, they recommend:
$ ssh-keygen -t ed25519 -C "your_email@example.com"
I slightly modified this to look like:
$ ssh-keygen -f dmrobbins03 -C "mygithubaccountemail@gmail.com"
-f is the encoding type, dmrobbins03 is the file name prefix (it will generate dmrobbins03_id_rsa).
One tutorial (https://www.youtube.com/watch?v=N2hMGEeYR7c) recommended -f for their keygen, and that looked more familiar to me than the -t output. I am confident that either one works; I was just frustrated at the time.
For me, my config file looked like this:
# Default - dmrobbins03
Host github.com
HostName github.com
IdentityFile ~/.ssh/dmrobbins03_id_rsa
You do NOT want to put your default GitHub SSH key under Host * – it will override everything!
I found this counter-intuitive since I assumed increased specificity would have priority. I guess not.
Be sure to follow all of the instructions on the first GitHub link. You’ll need to copy-paste your SSH keys to each of your accounts under Settings > SSH & GPG Keys.
Step 2: Create your second SSH key.
Follow the same instructions as above.
$ ssh-keygen -f clientdavid -C "myclientgithub@gmail.com"
My config then became:
# Default - dmrobbins03
Host github.com
HostName github.com
IdentityFile ~/.ssh/dmrobbins03_id_rsa
# Client Account
Host github.com-clientdavid
HostName github.com
IdentityFile ~/.ssh/clientdavid_id_rsa
Step 3: Verify
In terminal, use the following commands to verify your SSH is working with GitHub.
ssh -T git@github.com
If you’re successful, you’ll see this:
Hi dmrobbins03! You’ve successfully authenticated, but GitHub does not provide shell access.
Now try your alternate account.
ssh -T git@github.com-clientdavid
You should see this:
Hi client-david! You’ve successfully authenticated, but GitHub does not provide shell access.
THE IMPORTANT PART IS YOUR USERNAME.
I wasted more minutes than I should have because I did not notice that both verifications were using my default account. This was because I had set originally up my host to be *.
Now how do you use this damn thing?
Step 4: Using the damn thing.
When you clone a GitHub repository with SSH, your command will look something like this:
git clone git@github.com:someonesaccount/my-repository.git
That uses your default account. In order to use your secondary (tertiary, etc) accounts, your command will need to be modified as follows:
git clone git@github.com-clientdavid:someonesaccount/my-repository.git
Just change github.com to github.com-clientdavid and that’s it. You’ll have the correct authentication from this point forward.
Additional references: