Using SSH Host Aliases to Handle Multiple GitHub Accounts (Same github.com)

When working with multiple GitHub accounts (for example, office and personal) on the same machine, things get confusing quickly — especially when all repositories live on github.com.

This post explains how SSH Host Aliases solve this problem, step by step, with a clear mental model.


The Core Problem

  • You have multiple GitHub accounts
  • All repos are on the same host: github.com
  • You want to use SSH (not PATs)
  • You want correct attribution (office email for office OSS, personal email for personal repos)

The challenge:

How does SSH know which GitHub account to use?


Key Insight (Very Important)

SSH does not know about GitHub accounts, usernames, or emails.
SSH authentication is based ONLY on the SSH key presented.

Git commit email and GitHub username are not used for authentication.


Why git@github.com Is Not Enough

When you run:

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

Behind the scenes:

  1. Git invokes SSH with git@github.com
  2. SSH looks for a matching Host github.com entry
  3. SSH picks a default key (or the first loaded one)
  4. GitHub authenticates whichever account owns that key

❌ This breaks when you have multiple accounts.


What Is an SSH Host Alias?

An SSH Host Alias is:

  • A local-only nickname
  • Defined in ~/.ssh/config
  • Maps to:
    • the real host (github.com)
    • a specific SSH key

GitHub never sees the alias — only your machine does.


How Host Aliases Fix the Problem

Instead of using git@github.com, we use different aliases, each tied to a specific key.

Example aliases:

  • github-office
  • github-personal

Both still connect to github.com, but with different SSH keys.


Step-by-Step: How It Works Internally

1. Git parses the remote URL

git@github-office:org/repo.git

Git extracts:

  • user → git
  • host → github-office

2. Git calls SSH

Equivalent to:

ssh git@github-office

3. SSH reads ~/.ssh/config

Host github-office
  HostName github.com
  User git
  IdentityFile ~/.ssh/id_ed25519_office
  IdentitiesOnly yes

SSH now understands:

  • Real destination → github.com
  • SSH key to use → id_ed25519_office

4. SSH connects to GitHub

  • SSH connects to github.com
  • Sends only the office public key
  • GitHub finds which account owns that key
  • Authentication succeeds as the office account

✔ Alias never leaves your machine
✔ GitHub never sees your email
✔ GitHub only sees a public key


Why IdentitiesOnly yes Matters

Without it:

  • SSH may try multiple loaded keys
  • GitHub may reject after too many attempts
  • Or authenticate the wrong account

With it:

IdentitiesOnly yes

SSH rule becomes:

“Use ONLY the key specified for this host.”

This makes behavior predictable and safe.


Git Commit Email Is Separate from Authentication

After authentication:

  • Git pushes commits
  • Commits contain:
    • author name
    • author email

GitHub uses email only for display and attribution.

You can safely set per-repo identity:

git config user.name \"Office Name\"
git config user.email \"office@company.com\"

This has no effect on SSH authentication.


Why You Must Change Remote URLs

Each repository permanently stores its remote:

git remote -v

If it says:

git@github.com:org/repo.git

SSH cannot distinguish accounts.

You must change it to:

git@github-office:org/repo.git

This binds:

repo → host alias → SSH key → GitHub account

Mental Model (Simple Analogy)

Think of it like a building:

  • github.com → the building
  • SSH keys → access cards
  • Host aliases → labeled doors
Door: github-office   → office access card
Door: github-personal → personal access card

Both doors enter the same building.


Summary

  • SSH does not know GitHub accounts
  • SSH authentication is key-based only
  • Host aliases let you choose the correct key
  • GitHub maps key → account
  • Commit email is just metadata
  • Changing remote URLs is required (and normal)

Final Recommendation

Using SSH host aliases is:

  • Secure
  • Scalable
  • Industry standard
  • PAT-free
  • The only clean solution for multiple GitHub accounts on the same host

For some commandline tips you can go through less known commandline tips. You also might find AI model overfitting article interesting. Thanks for reading through, and have a nice day!

Follow for more!