How to Fix Git SSH Key Not Working Error
In this tutorial, you'll learn about How to Fix Git SSH Key Not Working Error. We cover key concepts, practical examples, and best practices.
You try to clone or push using SSH and get Permission denied (publickey) — your SSH key is not configured, not added to the SSH agent, or not registered with your Git hosting provider.
The Problem
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.
Step-by-Step Fix
Step 1: Check existing SSH keys
ls -la ~/.ssh/
Look for id_ed25519 or id_rsa files.
Step 2: Generate a new SSH key (if none exists)
ssh-keygen -t ed25519 -C "your-email@example.com"
Accept the default location and set a passphrase (optional).
Step 3: Start the SSH agent and add the key
eval "$(ssh-agent -s)"
ssh-add ~/.ssh/id_ed25519
Step 4: Copy the public key
cat ~/.ssh/id_ed25519.pub
Step 5: Add the key to GitHub
- Go to GitHub.com → Settings → SSH and GPG keys → New SSH key
- Paste the public key content
- Save
Step 6: Test the connection
ssh -T git@github.com
Expected:
Hi username! You've successfully authenticated, but GitHub does not provide shell access.
Step 7: Configure Git to use SSH
git remote set-url origin git@github.com:user/repo.git
Step 8: Use SSH config for multiple keys
Create ~/.ssh/config:
Host github.com
HostName github.com
User git
IdentityFile ~/.ssh/id_ed25519
Host gitlab.com
HostName gitlab.com
User git
IdentityFile ~/.ssh/gitlab-key
Prevention Tips
- Use Ed25519 keys instead of RSA (more secure and faster)
- Add keys to SSH agent automatically in
~/.bashrc - Register keys with all Git providers you use
- Back up your SSH keys securely
- Use different keys for different services if needed
Common Mistakes with ssh key error
- Using
foldlinstead offoldl'causing stack overflow on large lists - Forgetting
deriving (Show, Eq)on custom data types needed for debugging - Placing the wildcard pattern first in case expressions, making all subsequent patterns unreachable
These mistakes appear frequently in real-world GIT code. DodaTech's contributors have identified these patterns through analysis of open-source projects and production systems.
Practice Exercise
Write a pure function that safely divides two integers using Maybe, then test it with edge cases like division by zero and negative numbers.
This exercise reinforces the concepts covered in this guide. Try implementing it before checking online solutions.
FAQ
Built by the developers of DodaTech
Doda Browser, DodaZIP & Durga Antivirus Pro