Multiple GitHub accounts on one machine

I have 3 GitHub accounts: one for work, one for personal use, and one under the earendelmir pseudonym. The challenge is to manage these on the same machine, to seamlessly commit and push changes using the appropriate account, depending on the current repository.

This guide is mainly for my own reference, but I decided to publish it in the hope it might help others too.

Table of contents

  1. Create SSH key
  2. Add SSH key to GitHub
  3. Test your SSH connection
  4. Clone repository
  5. Configure repository
  6. Appendix A: sign commits with SSH key
  7. Appendix B: sign commits with GPG key
  8. Resources

1. Create SSH key

From now on, replace EVERY occurrence of 'username' and 'email@address.com' with your actual GitHub credentials.

For each account:

cd ~/.ssh
ssh-keygen -t ed25519 -C "email@address.com" -f "github-username"
ssh-add ~/.ssh/github-username

Keep the github- part in the key's name, just for clarity.
Also, make sure to use a secure passphrase.

Edit the ~/.ssh/config file by adding this block, for each account:

Host github.com-username
    HostName github.com
    User git
    IdentityFile ~/.ssh/github-username

2. Add SSH key to GitHub

For each account, copy its public key:

xclip -sel clip ~/.ssh/github-username.pub

Then, go to github.com, under Settings > SSH and GPG keys > New SSH key.
Give the key a title of your choice, paste the copied text, and leave the default key type: Authentication Key

3. Test your SSH connection

Type this in your terminal:

ssh -T git@github.com-username

Your output should be: "Hi username! You've successfully authenticated, but GitHub does not provide shell access".
You might be prompted to verify the authenticity of github's server; check that the fingerprint you see matches one of these and type yes.
Follow this guide if you get any error instead.

4. Clone repository

Use the SSH URL to clone a repository:

git clone git@github.com-username:RepoUser/RepoName.git

5. Configure repository

Set user name and email for the account you're going to use:

git config user.email email@address.com
git config user.name  username

Appendix A: Sign commits with SSH key

Since an SSH key is now available for each GitHub account, we might as well use it to sign commits too.
(please note that this idea might come with security risks I'm not aware of yet)

In order for this to work, you need to upload the ssh key to GitHub — yes, same key and same procedure as before, except that this time you need to set key type to: Signing Key.

Then, configure your repository to work with this SSH key:

git config commit.gpgsign true
git config gpg.format ssh
git config user.signingkey "~/.ssh/github-username.pub"

Note that this only works with Git 2.34 or later.


Appendix B: Sign commits with GPG key

In your repository, add the following configuration:

git config commit.gpgsign true
git config --unset gpg.format

If you already have a GPG key that matches your committer identity and your verified email address associated with your account on GitHub, then you don't need to do anything else.
If you want to use a different key, that is not linked to your GitHub email address, you need to specify its ID:

git config user.signingkey 12345ABCDE123450

You can find the ID of your keys with the following command:

gpg --list-secret-keys --keyid-format=long

Resources