GitHub: Key is Already in Use

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 git@github.com:shitty_user/shitty_repo.git 

# Do this instead to use the new SSH key
git clone git@github-enterprise.com:shitty_user/shitty_repo.git 

Comments

    • Choon-Chern Lim says:

      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

Your email address will not be published. Required fields are marked *