Fix 'Permission denied (publickey)' When Pushing to GitHub

Getting a ‘Permission denied (publickey)’ error when trying to push to GitHub? This error means GitHub can’t authenticate you because of an SSH key problem. Let’s fix it quickly.
Key Takeaways
- The error occurs when GitHub can’t authenticate your SSH connection
- Three main causes: missing SSH keys, keys not added to GitHub, or incorrect remote URL format
- Test your connection with
ssh -T git@github.com
to verify the fix works - SSH agent and config files help manage multiple accounts or persistent authentication
What This Error Means
The ‘Permission denied (publickey)’ error happens when Git tries to connect to GitHub using SSH, but the authentication fails. Think of SSH keys as a special password that lets your computer talk to GitHub securely.
This guide covers the three main causes and shows you exactly how to fix each one.
The Three Main Causes (And How to Fix Them)
1. No SSH Keys Created on Your Computer
Quick Check:
ls -la ~/.ssh
If you see “No such file or directory” or the folder is empty, you need to create SSH keys.
Fix: Generate a new SSH key pair:
ssh-keygen -t ed25519 -C "your-email@example.com"
Press Enter three times to accept the defaults. This creates two files:
~/.ssh/id_ed25519
(private key - keep this secret)~/.ssh/id_ed25519.pub
(public key - share with GitHub)
2. Public Key Not Added to GitHub
Even with SSH keys created, GitHub won’t recognize you until you add your public key to your account.
Fix: First, copy your public key:
cat ~/.ssh/id_ed25519.pub
Then add it to GitHub:
- Go to GitHub SSH settings
- Click “New SSH key”
- Paste your key and save
3. Wrong Remote URL Format
GitHub SSH URLs must start with git@github.com:
- not your email or username.
Check your current URL:
git remote -v
Correct format:
git@github.com:username/repository.git
Wrong formats:
youremail@github.com:username/repository.git ❌
https://github.com/username/repository.git ❌ (this is HTTPS, not SSH)
Fix: Update to the correct SSH URL:
git remote set-url origin git@github.com:username/repository.git
Discover how at OpenReplay.com.
Verify Everything Works
Test your SSH connection to GitHub:
ssh -T git@github.com
You should see:
Hi username! You've successfully authenticated...
If this works, you can now push to GitHub without the permission denied error.
Common Troubleshooting Issues
SSH Agent Not Running
If your key exists but still doesn’t work, the SSH agent might not be running.
Fix:
eval "$(ssh-agent -s)"
ssh-add ~/.ssh/id_ed25519
Multiple GitHub Accounts
Managing multiple accounts requires an SSH config file. Create ~/.ssh/config
:
Host github.com-work
HostName github.com
User git
IdentityFile ~/.ssh/id_ed25519_work
Host github.com-personal
HostName github.com
User git
IdentityFile ~/.ssh/id_ed25519_personal
Then clone using:
git clone git@github.com-work:company/repository.git
Platform-Specific Notes
- Windows users: Use Git Bash instead of Command Prompt or PowerShell
- macOS/Linux users: Use Terminal
Both platforms follow the same commands shown above.
Conclusion
The ‘Permission denied (publickey)’ error is frustrating but easy to fix. In most cases, you just need to create SSH keys and add the public key to GitHub. Remember to verify your connection with ssh -T git@github.com
before pushing.
Once set up correctly, you’ll never see this error again for that repository.
FAQs
While technically possible, it's best practice to create separate key pairs for each account. This improves security and makes troubleshooting easier when managing different repositories.
SSH is more convenient once configured since you don't need passwords for each push. HTTPS works everywhere but requires authentication tokens or passwords for every operation.
No. Add your key to the SSH agent using ssh-add to cache the passphrase for your session. This way you only enter it once per session.
Immediately generate a new key pair and update GitHub with the new public key. Remove the compromised key from GitHub settings and never share private keys.
Understand every bug
Uncover frustrations, understand bugs and fix slowdowns like never before with OpenReplay — the open-source session replay tool for developers. Self-host it in minutes, and have complete control over your customer data. Check our GitHub repo and join the thousands of developers in our community.