Description:
If you're seeingPermission denied (publickey)
when trying to push to GitHub using Git and SSH on Windows, you're not alone. This guide covers every possible cause of the error and walks you through step-by-step solutions using PowerShell, including SSH key generation, agent setup, and GitHub configuration.
🐛 Common Errors and What They Mean
Description: These are typical errors developers face when SSH isn't correctly configured with GitHub.
git@github.com: Permission denied (publickey).
fatal: Could not read from remote repository.
Or, when testing the SSH connection:
ssh -T git@github.com
git@github.com: Permission denied (publickey).
Or while starting the SSH agent:
Start-Service : Cannot start service ssh-agent on computer '.'
Error: 1058
✅ Step 1: Check if You Already Have an SSH Key
Description: Check if an SSH key already exists in your system.
Run in PowerShell:
dir $env:USERPROFILE\.ssh
// USERPROFILE: C:Users\Username
Look for files named:
id_ed25519
id_ed25519.pub
If they're missing, proceed to Step 2 to generate them.
🔐 Step 2: Generate a New SSH Key
Description: If you don’t have an SSH key, create one using the command below.
ssh-keygen -t ed25519 -C "your_email@example.com"
Press Enter to accept the default file path.
Optionally set a passphrase (or leave it blank for no passphrase).
This generates two files:
Private key: id_ed25519
Public key: id_ed25519.pub
⚙️ Step 3: Enable and Start the SSH Agent (Properly on Windows)
Description: On Windows, the SSH agent must be enabled and started using PowerShell.
Run these in PowerShell as Administrator:
Set-Service -Name ssh-agent -StartupType Manual
Start-Service ssh-agent
Then add your SSH key:
ssh-add $env:USERPROFILE\.ssh\id_ed25519
You should see:
Identity added: C:\Users\YourName\.ssh\id_ed25519
🔗 Step 4: Add Your SSH Key to GitHub
Description: GitHub must have your public key to allow authentication.
Get-Content $env:USERPROFILE\.ssh\id_ed25519.pub
Copy the entire output, which looks like:
ssh-ed25519 AAAAC3NzaC1lZDI1NTE5AAAAIK... user@example.com
Then:
Go to GitHub SSH Keys Settings
Click New SSH key
Title: Something like Windows Dev Machine
Key: Paste the entire key
Click Add SSH key
🧪 Step 5: Test Your SSH Connection to GitHub
Description: This verifies whether your SSH setup is correctly linked to GitHub.
ssh -T git@github.com
Expected output:
Hi your-username! You've successfully authenticated...
🚀 Step 6: Push Your Code
Description: Now that SSH is configured, retry your Git command.
git push -u origin main
If everything is set up correctly, your code should push without issues.
🧾 Summary of All Possible Errors and Fixes
❌ Error | 💡 Cause | 🛠️ Solution |
---|---|---|
Permission denied (publickey) |
GitHub doesn't recognize your SSH key | Add your public key to GitHub |
Could not read from remote repository |
Auth failed or repo not found | Check SSH key, repo URL, and existence |
ssh-agent cannot be started (error 1058) |
Agent service is disabled | Run Set-Service and Start-Service as Admin |
eval not recognized |
Unix command used in PowerShell | Use PowerShell-native commands instead |
No such file or directory on ssh-add
|
SSH key doesn’t exist | Generate a new key using ssh-keygen
|
🙋 Troubleshooting Tips
Make sure you're using SSH URLs, not HTTPS.
- SSH: git@github.com:username/repo.git ✅
- HTTPS: https://github.com/username/repo.git ❌
To see which key is being used, run:
ssh -vT git@github.com
Ensure you’re using the correct GitHub account if you have multiple.
📌 Final Thoughts
SSH configuration on Windows can be tricky, especially if you're used to Unix-based systems. But with the right steps — generating keys, configuring the agent, and adding keys to GitHub — you can be up and running securely in minutes.
Happy coding! 🚀
Top comments (0)