Skip to main content

Git · 5 common causes

Permission denied (publickey)

Updated

The SSH server rejected every key your client offered, so it never authenticated you.

The exact message

$ git clone git@github.com:you/project.git
Cloning into 'project'...
git@github.com: Permission denied (publickey).
fatal: Could not read from remote repository.

Please make sure you have the correct access rights
and the repository exists.

Paths, line numbers and stack frames differ on every machine, and the wording is tightened between versions. The part that identifies the problem is Permission denied (publickey).

How to fix it

Cause 1 of 5

No key exists, or the public key is not on your account.

Test first: ssh -T git@github.com names the account when it works. If it fails, generate a key with ssh-keygen -t ed25519 -C "you@example.com" and add the contents of ~/.ssh/id_ed25519.pub in the host's SSH keys settings. Upload only the .pub file — the file without the extension is the private key and must never leave your machine.

Cause 2 of 5

The agent is not running, or does not hold the key.

eval "$(ssh-agent -s)" then ssh-add ~/.ssh/id_ed25519. On macOS, ssh-add --apple-use-keychain ~/.ssh/id_ed25519 keeps it loaded across restarts.

Cause 3 of 5

The key has a non-default filename and nothing maps it to the host.

SSH only tries the standard names on its own. Add an entry to ~/.ssh/config: a Host github.com block with IdentityFile ~/.ssh/my_key and IdentitiesOnly yes, so it offers that key and stops after it.

Cause 4 of 5

The private key file permissions are too open.

SSH refuses to use a private key that other users can read. chmod 600 ~/.ssh/id_ed25519 and chmod 700 ~/.ssh are the correct modes — tightening, not loosening. A key at 777 is exactly what SSH is protecting you from.

Cause 5 of 5

The key belongs to a different account, or you only have HTTPS access.

ssh -vT git@github.com prints every key offered and the server's reply, which identifies the wrong-account case immediately. Switching the remote to HTTPS with a personal access token is a valid alternative: git remote set-url origin https://github.com/you/project.git.

Stopping it coming back

Keep one Ed25519 key per machine, add it to the agent from your shell profile, and record host-specific keys in ~/.ssh/config so the right one is offered without thinking about it.

Permission denied (publickey) — common questions

Corporate network blocks port 22. Any way around it?
GitHub also serves SSH on port 443. Add a Host github.com block to ~/.ssh/config with Hostname ssh.github.com and Port 443, then test with ssh -T git@github.com. Other hosts often provide something similar.
Should I use SSH or HTTPS for Git?
Both are fine. SSH avoids repeated credential prompts once the key is set up; HTTPS with a token works through more restrictive networks and is easier on shared machines. Pick per environment rather than as a rule.
How do I use different keys for work and personal accounts?
Define two Host aliases in ~/.ssh/config — for example Host github-work with the work IdentityFile — and use the alias in the remote URL: git@github-work:org/repo.git. Each repository then authenticates as the right identity.

Test the fix before you paste it back

Reproducing the failure in a scratch file is usually faster than editing the real one. AltF Code is a free in-browser editor with real syntax highlighting for HTML, CSS, JavaScript, TypeScript, Python, SQL and JSON, and it runs HTML, CSS and JavaScript live in a preview panel. No install and no sign-up.

Open the editor