GitHub / 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 [email protected]
# Expected: Hi <username>! You've successfully authenticated...
When this happens
You run git push, git pull, or git clone over SSH and GitHub shows a publickey error such as:
[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.
Git cannot authenticate because it cannot find or use your SSH private key. This is the common GitHub "permission denied publickey" failure.
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 "[email protected]"
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 [email protected]:user/repo.git
Related