More Git answers

by Danny
Published 3 min read

GitHub rejected your SSH key when you pushed or cloned. Learn how to check which key is being offered, add a key to GitHub, and load it into the SSH agent.

The short answer

#

GitHub could not match any SSH key your machine offered to a key on your GitHub account. Test the connection, and if it fails, create a key (or find your existing one), add the public half to GitHub, and make sure the SSH agent is offering it.


_10
# If that fails:
_10
ssh-keygen -t ed25519 -C "[email protected]"
_10
ssh-add ~/.ssh/id_ed25519
_10
cat ~/.ssh/id_ed25519.pub # paste this into GitHub, Settings, SSH and GPG keys

A successful test prints a greeting with your username and says GitHub does not provide shell access. That message is expected.

Why it happens

#

When your remote URL starts with [email protected]:, Git talks to GitHub over SSH. SSH proves who you are with a key pair: a private key that stays on your machine and a public key you register with GitHub. The full error usually reads "[email protected]: Permission denied (publickey). fatal: Could not read from remote repository."

It appears when:

  • You have no SSH key at all, which is common on a new machine.
  • You have a key, but the public half was never added to your GitHub account.
  • The key exists but is not loaded into the SSH agent, often after a reboot, or it has a non-standard file name SSH does not try by default.
  • SSH is offering the wrong key, for example a work key on a personal repository.
  • You have access to the repository through a different account from the one the key belongs to.
  • You run Git with sudo, which uses root's keys, not yours.

How to fix it

#

See what SSH is doing

The verbose flag shows which key files SSH tries and which one GitHub accepts or rejects:

Look for lines mentioning "Offering public key" and "Authenticated". If no key is offered, SSH cannot find one. If one is offered and refused, that key is not on the account you need.

Create a key if you need one


_10
ls -al ~/.ssh # look for id_ed25519.pub or id_rsa.pub
_10
ssh-keygen -t ed25519 -C "[email protected]"

Accept the default location and set a passphrase. Ed25519 is the type GitHub recommends.

Add the public key to GitHub

Copy the contents of the .pub file, never the private key, and paste it into GitHub under Settings, then SSH and GPG keys, then New SSH key. On macOS, pbcopy puts it on the clipboard:


_10
pbcopy < ~/.ssh/id_ed25519.pub

If you use the GitHub CLI, gh ssh-key add ~/.ssh/id_ed25519.pub does the same thing.

Load the key into the agent


_10
eval "$(ssh-agent -s)"
_10
ssh-add ~/.ssh/id_ed25519
_10
ssh-add -l # list loaded keys

On macOS, store the passphrase in the keychain so the key loads automatically, by adding this to ~/.ssh/config:


_10
Host github.com
_10
AddKeysToAgent yes
_10
UseKeychain yes
_10
IdentityFile ~/.ssh/id_ed25519

Then run ssh-add --apple-use-keychain ~/.ssh/id_ed25519 once.

Pick the right key for each account

If you have separate work and personal accounts, give each a host alias in ~/.ssh/config and point the remote at the alias:


_10
Host github-work
_10
HostName github.com
_10
User git
_10
IdentityFile ~/.ssh/id_ed25519_work
_10
IdentitiesOnly yes


_10
git remote set-url origin git@github-work:my-company/app.git

Or switch to HTTPS

If you would rather not use SSH, change the remote to HTTPS and sign in with the GitHub CLI or Git Credential Manager:


_10
git remote set-url origin https://github.com/owner/repo.git
_10
gh auth login

Port 22 blocked

Some networks block SSH. GitHub also accepts SSH on port 443 through ssh.github.com. Test it with ssh -T -p 443 [email protected], and if it works, set Hostname ssh.github.com and Port 443 for github.com in your SSH config.

Common pitfalls

#
  • Pasting the private key into GitHub. Only the .pub file goes there. If you shared a private key, delete it and make a new one.
  • Running sudo git push. Root has its own ~/.ssh, so your keys are not used.
  • Adding the key as a deploy key on one repository and expecting it to work for others. Deploy keys are tied to a single repository.
  • Wrong file permissions. SSH ignores a private key that other users can read; chmod 600 the key and chmod 700 the .ssh folder.
  • Having access through an organisation that requires SAML single sign-on. You need to authorise the key for that organisation in your GitHub key settings.
#

Was this resource helpful?