git permission denied (publickey) — how to fix
Quick Answer
# 1. Start ssh-agent and load your key
eval "$(ssh-agent -s)"
ssh-add ~/.ssh/id_ed25519
# 2. Copy your public key — paste it into GitHub / GitLab
cat ~/.ssh/id_ed25519.pub
# GitHub → Settings → SSH and GPG keys → New SSH key
# GitLab → Preferences → SSH Keys → Add new key
# 3. Verify the connection
ssh -T git@github.com
# Expected: Hi <username>! You've successfully authenticated...
When this happens
You run git push, git pull, or git clone over SSH and see:
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.
Git cannot authenticate because it cannot find or use your SSH private key.
Other causes & fixes
No SSH key exists yet
# Check if you already have a key
ls ~/.ssh/id_*.pub
# If nothing is listed, generate one
ssh-keygen -t ed25519 -C "you@example.com"
macOS: key lost after reboot
On macOS the ssh-agent resets on restart. Add this to ~/.ssh/config to load the key automatically via Keychain:
Host *
AddKeysToAgent yes
UseKeychain yes
IdentityFile ~/.ssh/id_ed25519
Remote URL is HTTPS, not SSH
If the remote URL starts with https://, Git uses a password/token prompt instead of SSH keys.
# Check current URL
git remote -v
# Switch to SSH
git remote set-url origin git@github.com:user/repo.git
Related