๐Ÿ” Fixing “Permission Denied (publickey)” While Using Multiple GitHub Accounts

If you’ve ever tried cloning a repository and encountered this error:

git@github.com: Permission denied (publickey).
fatal: Could not read from remote repository.

you’re not alone. This commonly happens when using multiple GitHub accounts with SSH keys. The issue usually isn’t with the repository — it’s with which SSH key Git is using.


In this blog, we’ll break down:

  • Why this error happens

  • How SSH selects keys

  • How to properly configure multiple GitHub accounts

  • The clean, professional fix


๐Ÿšจ Why the Error Happens


When you run:

git clone git@github.com:username/repo.git

SSH uses the default configuration for github.com.


If you have multiple SSH keys (e.g., personal, college, work), SSH may try the wrong key. GitHub then rejects the authentication request because that key is not associated with the target account.


๐Ÿ” Identifying the Problem


Run:

ssh -T git@github.com -v

Look for:

Offering public key: /Users/yourname/.ssh/id_ed25519

If the wrong key is being offered, GitHub will deny access.


✅ The Proper Multi-Account Solution


The clean solution is to configure separate SSH host aliases for each GitHub account.


Step 1: Edit SSH Config


Open:

nano ~/.ssh/config

Add a dedicated host entry:

Host github-username
    HostName github.com
    User git
    IdentityFile ~/.ssh/id_ed25519_username
    IdentitiesOnly yes

This tells SSH:

  • Use this specific key

  • Only use this identity

  • Route traffic to GitHub


Step 2: Test Authentication

ssh -T git@github-username

Successful output:

Hi username! You've successfully authenticated.


Step 3: Clone Using the Alias


Instead of:

git clone git@github.com:username/repo.git

Use:

git clone git@github-username:username/repo.git

This forces Git to use the correct SSH key.


๐ŸŽฏ Why This Is the Best Approach


✔ No key conflicts

✔ Clean separation between accounts

✔ Scalable for work + personal + college

✔ Secure and production-ready setup


๐Ÿง  Pro Tip: Set Repository-Specific Identity


After cloning:

git config user.name "Your Name"
git config user.email "your-email@example.com"

This prevents commits from being pushed under the wrong GitHub account.


๐Ÿ Final Thoughts


The “Permission denied (publickey)” error isn’t a Git problem — it’s an SSH configuration issue.


Once you properly configure host aliases in ~/.ssh/config, you can seamlessly manage multiple GitHub accounts without conflicts.


If you work across personal, academic, and professional environments, this setup is essential.


If you’d like, I can also create:

  • A Medium-ready formatted version

  • A LinkedIn technical post

  • A beginner-friendly version

  • Or a DevOps-focused advanced explanation ๐Ÿš€

Comments