Embracing the Messiness in Search of Epic Solutions

GitHub: Key is Already in Use

Posted

in

PROBLEM

When trying to add a SSH key in GitHub, an error “Key is already in use” is thrown even though you signed into GitHub using a different user account.

SOLUTION

The error occurs when the same SSH key has been added to a different account. This typically happens when you already have an existing personal GitHub account and you are trying to use the same SSH key in a work-related GitHub Enterprise account.

The fix is to use a new SSH key. When generating the SSH key using ssh-keygen, remember NOT to override the existing ~/.ssh/id_rsa. Rather, use a different file path, such as ~/.ssh/id_rsa_github_enterprise.

To ensure seamless integration when doing a git clone, add the following configuration in ~/.ssh/config.

Host github.com
  Hostname github.com
  User git
  IdentityFile ~/.ssh/id_rsa

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

The first block ensures it doesn’t break the existing GitHub connectivity using the default SSH key.

The second block allows a different SSH key to be used when connecting to GitHub. In order to leverage this configuration, the host name needs to be tweaked when doing a git clone.

# Don't do this because this will use ~/.ssh/id_rsa
git clone [email protected]:shitty_user/shitty_repo.git 

# Do this instead to use the new SSH key
git clone [email protected]:shitty_user/shitty_repo.git 

Tags:

Comments

2 responses to “GitHub: Key is Already in Use”

  1. RemyG Avatar

    Hi, I’ve recently written a very similar blog post, but with another solution which doesn’t require you to tweak the repository URL, but instead work with local folder paths: https://remyg.fr/blog/2022/11/17/using-multiple-ssh-keys-concurrently/

    1. Choon-Chern Lim Avatar

      Fantastic! Thank you for sharing! I currently have folder-based .gitconfig to take care of the user email, but I didn’t know there is an additional construct to handle the SSH key configuration, which I think is a more elegant solution.

Leave a Reply