Programming

git push fails with 'Permission denied (publickey)' after adding new SSH key to laptop

Environment

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

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

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

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

Diagnostic steps

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

Confirmed fixes

No fix has been independently verified for this scenario. Treat the causes below as diagnostic hypotheses.

Question

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?

COMMUNITY RESPONSES

Verified community troubleshooting

1 reply
0
viktorbyte Answer
user
Posted 4w ago • 0 confirmations

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.

Showing 1 of 1 community responses
AI DIAGNOSTICS

Incident analysis

Updated 4w ago• Trouble ID

Most likely causes

ssh-agent not loaded / key not offered 8%
SSH config (Host/IdentityFile) points to different key or overrides agent 6%
Wrong key uploaded to remote account (wrong file/content or in different account) 6%

Summary

The server refused key-based auth. Likely causes: agent not offering the right key, wrong IdentityFile or Host entry in ~/.ssh/config, key added to wrong account, or the remote rejecting key due to format/permissions. Diagnostics must confirm which side fails by testing an explicit key and examining verbose SSH negotiation.

Suggested Fixes

Diagnostic step 1
Run: ssh -v -i ~/.ssh/id_rsa git@github.com Result meaning: 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.
Diagnostic step 2
List agent identities: ssh-add -l (or start agent and ssh-add then ssh-add -l). Result meaning: 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.
Diagnostic step 3
Temporarily test an isolated connection: ssh -o IdentitiesOnly=yes -i ~/.ssh/id_rsa -v git@github.com Result meaning: 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.

Continue troubleshooting

Matched from the title, description and tags