When connecting to a remote Git server, like Github, your client must authenticate using HTTPS or SSH based tokens. If you’re using the latter, you may run into issues with key handling, including Git using the wrong SSH key and signing you into a different account.

What’s The Problem?

If you don’t have an SSH key at all on your account, you’ll almost certainly get the following error when trying to push:

This is probably the most common Git mistake, and the solution to this is simple—set up an SSH key for your account.

But, you can also run into a very similar looking error message, even after having an SSH key set up properly. If you use multiple Git accounts, you may have this error:

In this case, Github has accepted your SSH key, but it’s still not working correctly; even though your local git repo is configured to use the right email address, if you give it the wrong key, it will authenticate you as the wrong user. This is fundamentally different from the first error. It is not “permission denied,” but instead “permission denied to account.”

This can be a problem if you have multiple accounts and need to use separate keys for each of them. Due to the way SSH authentication works, it will still “sign you in” to Git’s remote server, but as the wrong user. When Github checks if that user has access to the repository, it will likely fail, thus producing the error saying your account does not have permission to use your own repo.

The solution is to properly manage multiple SSH keys, so that your git client doesn’t send the wrong key to the server. You can do this by editing SSH’s configuration files.

Making Sure Git Is Configured Properly

First off, you’ll want to make sure your repo is configured to use the right email address and username. You can check that from the Git config:

And set the name and email for this repo (or using –global to set it for all repos):

If you’re not having SSH key issues, this may be all that is required.

Editing ~/.ssh/config

If you don’t have an existing key you’d like to use already, you will need to generate a new key file for your account. If you do, you’ll need to move or rename the proper one into ~/.ssh so it does not conflict with your default id_rsa. In this case, we’ll create a new key named “github”:

ssh-keygen -t rsa -f ~/.ssh/github

To use multiple SSH keys, you will need to edit ~/.ssh/config and set up a new Host block for Github that points to the keyfile you just created. SSH will use this to select the SSH key it will use.

If you only have the one host block for the new key, you shouldn’t have to do anything else, and will be able to connect to Github now. If you have multiple blocks for different keys, you’ll need to edit your repository’s remote so that github.com is replaced by the name of the host block in SSH’s config file. This will manually select a key rather than relying on a conflicting lookup.

Troubleshooting

After setting up SSH host configuration and configuring your remotes, Git should use the right SSH key when pushing. If you’re still having problems, you can debug the issue by manually setting the SSH command that Git uses for a shell session using the GIT_SSH_COMMAND environment variable.

You’ll also want to check and make sure Git is using the key from the place you’re expecting. On Windows, this location can vary wildly depending on what software you’re using Git through. For example, using the GUI client Fork, it defaulted to the Windows SSH agent, but I had configured a new SSH key which it was using instead.