---
title: "git@github.com: Permission denied (publickey)"
description: "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."
url: https://www.danny.engineering/answer/git/gitgithubcom-permission-denied-publickey
author: Danny
published: 2026-11-27T00:00:00.000Z
updated: 2026-11-27T17:24:00.000Z
tags: ["Git"]
---

# git@github.com: Permission denied (publickey)

## 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.


```bash
ssh -T git@github.com
# If that fails:
ssh-keygen -t ed25519 -C "you@example.com"
ssh-add ~/.ssh/id_ed25519
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 `git@github.com:`, 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 "[git@github.com](mailto:git@github.com): 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:


```bash
ssh -vT git@github.com
```


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


```bash
ls -al ~/.ssh                      # look for id_ed25519.pub or id_rsa.pub
ssh-keygen -t ed25519 -C "you@example.com"
```


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:


```bash
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


```bash
eval "$(ssh-agent -s)"
ssh-add ~/.ssh/id_ed25519
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`:


```bash
Host github.com
  AddKeysToAgent yes
  UseKeychain yes
  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:


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


```bash
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:


```bash
git remote set-url origin https://github.com/owner/repo.git
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 git@ssh.github.com`, 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.

## Related reading

- [GitHub Docs: Error: Permission denied (publickey)](https://docs.github.com/en/authentication/troubleshooting-ssh/error-permission-denied-publickey)
- [GitHub Docs: Generating a new SSH key and adding it to the ssh-agent](https://docs.github.com/en/authentication/connecting-to-github-with-ssh/generating-a-new-ssh-key-and-adding-it-to-the-ssh-agent)
