Managing Multiple GitHub Accounts with SSH: A Complete Guide

We’ve all been there. You have a personal GitHub account for your side projects, and a professional one for your day job. You’re firing on all cylinders, ready to commit some code, and—BAM.


ERROR: Permission to Organization/Repo.git denied to User-X.
fatal: Could not read from remote repository.

Wait, what? You own that repository! But for some reason, your machine is stubbornly trying to authenticate as the wrong user. It’s the ultimate "why is this happening to me?" moment.

Understanding the "Why" (Before You Break Anything)

The biggest mistake I made is thinking that authentication is one big, singular thing. It’s actually two separate layers:

  • The SSH Layer: This is the bouncer at the door. It handles the handshake between your computer and GitHub.
  • The Git Layer: This is the accountant. It decides which identity (email and name) to slap on your commit, and specifically which key to hand to the SSH bouncer.

The "It Actually Works" Step-by-Step

Step 1: Create Custom Keys

ssh-keygen -t ed25519 -C "your_email_1@example.com" -f ~/.ssh/id_ed25519_account1
ssh-keygen -t ed25519 -C "your_email_2@example.com" -f ~/.ssh/id_ed25519_account2

Step 2: Add Keys to the Agent

eval "$(ssh-agent -s)"
ssh-add ~/.ssh/id_ed25519_account1
ssh-add ~/.ssh/id_ed25519_account2

Step 3: The Secret Sauce: ~/.ssh/config

Host github-account1
  HostName github.com
  User git
  IdentityFile ~/.ssh/id_ed25519_account1

Host github-account2
  HostName github.com
  User git
  IdentityFile ~/.ssh/id_ed25519_account2

Step 4: Force Git to Behave

git config core.sshCommand "ssh -i ~/.ssh/id_ed25519_account1"

Step 5: Update Your Remote URL

git remote set-url origin git@github-account1:username/repo.git

It might seem like a bit of a setup process, but once it’s done, you can stop stressing about permission errors. No more "Permission Denied" panics—just clean, correctly attributed commits.

Happy coding!

Comments