Skip to content

Instantly share code, notes, and snippets.

@toasterparty
Last active January 21, 2025 18:21
Show Gist options
  • Save toasterparty/2d30d944d0b2e0974432a370e208419e to your computer and use it in GitHub Desktop.
Save toasterparty/2d30d944d0b2e0974432a370e208419e to your computer and use it in GitHub Desktop.

Coexist Multiple GitHub SSH Keys for Multiple Accounts

This guide applies to you if:

  1. You use SSH authentication for git

  2. You use more than one account, and thus require more than one SSH key in your ssh configuration file

Create Keys

# Generate key pair for Account 1
ssh-keygen -t ed25519 -C "[email protected]"
# Save it as ~/.ssh/id_ed25519_account1

# Generate key pair for Account 2
ssh-keygen -t ed25519 -C "[email protected]"
# Save it as ~/.ssh/id_ed25519_account2

# start the ssh-agent in the background
eval "$(ssh-agent -s)"

# add keys to agent
ssh-add ~/.ssh/id_ed25519_account1
ssh-add ~/.ssh/id_ed25519_account2

Add to appropriate accounts

https://docs.github.com/en/authentication/connecting-to-github-with-ssh/adding-a-new-ssh-key-to-your-github-account

Setup SSH Config File

Edit your SSH config file (typically ~/.ssh/config) and add entries for both keys

# Account 1
Host github.com-account1
    HostName github.com
    User git
    IdentityFile ~/.ssh/id_ed25519_account1

# Account 2
Host github.com-account2
    HostName github.com
    User git
    IdentityFile ~/.ssh/id_ed25519_account2

Choose an account for each repository

Each time you clone a new repository, you need to pick which account to use with either:

git remote set-url origin [email protected]:<username>/<repository-name>.git

or

git remote set-url origin [email protected]:<username>/<repository-name>.git

...replacing <username> with the github username and <repository-name> with the name of the repository.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment