Introduction
Git over SSH prevents password leaks. If your SSH key is missing or unregistered, you see:
Permission denied (publickey).
fatal: Could not read from remote repository.
This guide covers common causes and fixes on Windows.
What Is the Error?
- Occurs when the SSH client can’t find a valid key for the host.
- Git cannot authenticate you to GitHub, GitLab, Bitbucket, etc.
Common Causes
- No SSH key generated.
- SSH agent not running or key not added.
- Public key not uploaded to Git host.
- Wrong file permissions or config settings.
Solution 1: Generate a New SSH Key
- Open Git Bash.
-
Run:
ssh-keygen -t ed25519 -C "you@example.com" - Press Enter to accept defaults.
- (Optionally) set a passphrase.
Solution 2: Add Your Key to the SSH Agent
In Git Bash
eval "$(ssh-agent -s)"
ssh-add ~/.ssh/id_ed25519
In PowerShell
-
Start the agent:
Start-Service ssh-agent -
Add the key (adjust path):
ssh-add C:\Users\<YourUser>\.ssh\id_ed25519
Solution 3: Upload Your Public Key to Git Host
-
Copy your public key:
cat ~/.ssh/id_ed25519.pub - Log in to GitHub/GitLab/Bitbucket.
- Go to Settings → SSH and GPG keys → New SSH key.
- Paste the key and save.
Solution 4: Verify the SSH Connection
Run in Git Bash or PowerShell:
ssh -T git@github.com
Expected response:
Hi <username>! You've successfully authenticated.
Additional Tips
-
Check
~/.ssh/config:Host github.com User git HostName github.com IdentityFile ~/.ssh/id_ed25519 -
File permissions: Ensure private key is only readable by you. In Git Bash:
chmod 600 ~/.ssh/id_ed25519 -
Multiple keys: Use
ssh-add -lto list loaded keys.
Conclusion
Generating an SSH key, adding it to the agent, and registering it with your Git host resolves “Permission denied (publickey)” errors. Follow each step carefully on Windows.
Leave a comment