git push fails with 'Permission denied (publickey)' after adding new SSH key to laptop
Environment
macOS 13.6 laptop, OpenSSH 8.8p1, Git 2.39.2, remote repo hosted on GitHub under personal account. New RSA 4096 key generated and purportedly added to GitHub web UI.
Symptoms
git push origin main immediately errors: 'git@github.com: Permission denied (publickey). fatal: Could not read from remote repository.' No HTTP auth prompts; HTTPS remote works when switched.
Observations
ssh -v git@github.com shows 'Offering public key: /Users/me/.ssh/id_rsa' then 'Authentications that can continue: publickey' and finally 'Permission denied (publickey)'. ~/.ssh permissions are 700 and private key 600. Key present in GitHub web UI under SSH keys. ssh-agent showed no identities until ssh-add was run manually.
Already attempted
Regenerated key, re-added to GitHub web UI, restarted ssh-agent, ssh-add ~/.ssh/id_rsa, tried git push again — same error. Switched remote to HTTPS and push succeeds. Verified no corporate SSH proxy or bastion in use.
Diagnostic steps
- Run: ssh -v -i ~/.ssh/id_rsa git@github.com
- What the result means: If authentication succeeds (GitHub prints a welcome line), local key works. If verbose output still shows 'Permission denied', the remote rejected that key — check remote account key material or try a different key file.
- List agent identities: ssh-add -l (or start agent and ssh-add then ssh-add -l).
- What the result means: If no identities listed, agent isn't offering keys; add key with ssh-add and retry. If identity shown but SSH still fails, agent keys may be ignored by ~/.ssh/config or SSH is not using the agent.
- Temporarily test an isolated connection: ssh -o IdentitiesOnly=yes -i ~/.ssh/id_rsa -v git@github.com
- What the result means: IdentitiesOnly forces usage of the specified key. Success here indicates another key was being offered earlier confusing the server; failure indicates the specified key is not accepted by the server.
- From GitHub/GitLab web UI, re-copy the public key string and locally run: grep -xFf <(echo 'ssh-rsa AAAA...') ~/.ssh/id_rsa.pub
- What the result means: If the echoed public key exactly matches your local .pub, the uploaded key is correct. A mismatch implies uploaded wrong key or corrupted copy.
- Check remote host alias in ~/.ssh/config for Host github.com entries and IdentityFile or ForwardAgent settings.
- What the result means: If config overrides IdentityFile or sets IdentitiesOnly, SSH may not try agent keys. Remove or adjust conflicting entries and retry. If no config, default behavior applies.
Confirmed fixes
No fix has been independently verified for this scenario. Treat the causes below as diagnostic hypotheses.
Question
What specific checks will reliably distinguish between a missing/incorrect local key, agent/config issue, or a bad key installed on the remote account? How to safely test and repair without exposing private key material?
Verified community troubleshooting
Verify where the failure occurs by forcing a single key and checking SSH's verbal handshake. Run ssh -o IdentitiesOnly=yes -i ~/.ssh/id_rsa -v git@github.com — success returns GitHub's welcome message; failure shows the server rejecting that exact key and points to the remote account. If agent shows no identities (ssh-add -l), add the key and re-run. If the local public key differs from what you uploaded, remove the uploaded key and paste the exact ~/.ssh/id_rsa.pub contents into your account. For lab environments, use a throwaway repo to avoid exposing private data; always keep a local backup of any removed remote keys in case you must revert.
Incident analysis
Most likely causes
Summary
Suggested Fixes
Continue troubleshooting
Related troubles
Matched from the title, description and tags
- Programming46% match
Git push fails with 'Permission denied (publickey)' after adding SSH key to remote account
Open0 answers - Programming39% match
Permission denied (publickey) when pushing to remote over SSH after adding a new key
Open1 answer - Databases23% match
PostgreSQL WAL archiving fails with 'archive_command' permission denied after automated logrotate changed owner
Open0 answers - macOS23% match
Thunderbolt dock fails EDID read after lid‑close wake — external monitor reports 0Hz or black on macOS 14
Open0 answers - Windows22% match
Scheduled task runs interactively but fails under SYSTEM after domain migration with 'access denied' to network resource
Open0 answers