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.
_10ssh -T [email protected]_10# If that fails:_10ssh-keygen -t ed25519 -C "[email protected]"_10ssh-add ~/.ssh/id_ed25519_10cat ~/.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:
_10ssh -vT [email protected]
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
_10ls -al ~/.ssh # look for id_ed25519.pub or id_rsa.pub_10ssh-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:
_10pbcopy < ~/.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
_10eval "$(ssh-agent -s)"_10ssh-add ~/.ssh/id_ed25519_10ssh-add -l # list loaded keys
On macOS, store the passphrase in the keychain so the key loads automatically, by adding this to ~/.ssh/config:
_10Host 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:
_10Host github-work_10 HostName github.com_10 User git_10 IdentityFile ~/.ssh/id_ed25519_work_10 IdentitiesOnly yes
_10git 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:
_10git remote set-url origin https://github.com/owner/repo.git_10gh 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
.pubfile 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 600the key andchmod 700the.sshfolder. - 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.
Related reading
#Was this resource helpful?