SSH keys identify you to GitHub without passwords: you generate a key pair, upload the public key to an account, and Git uses the private key when pushing over SSH. For two GitHub accounts the rule is simple and strict — one key pair per account, GitHub rejects the same public key on two accounts ("Key already in use") — plus a small ~/.ssh/config file that tells Git which key to offer per host alias. This guide walks from zero: generating keys, adding them to both accounts, testing each with ssh -T, and pointing two repositories at the right identity.
id_ed25519-work for account A and id_ed25519-personal for account B; upload each public key to its account; define host aliases github-work / github-personal in ~/.ssh/config with IdentitiesOnly yes; clone with git@github-work:owner/repo.git. No shared key, no ambiguity.
What an SSH key actually is
An SSH key pair is two mathematically linked files: a private key (stays on your machine, like a password) and a public key (uploaded to GitHub, like a username). When you push to git@github.com:..., the SSH client proves possession of the private key; GitHub checks whether the matching public key is authorized on an account. No password prompt, token, or browser — just the key.
Step 1 — generate one key pair per account
# on Windows: Git Bash
ssh-keygen -t ed25519 -C "me@company.com" -f ~/.ssh/id_ed25519-work
ssh-keygen -t ed25519 -C "me@personal.com" -f ~/.ssh/id_ed25519-personal
-Cis only a label — put the account's email so you can tell the keys apart later.-fkeeps the pairs in separate files; this is the entire trick.- Passphrases are recommended —
ssh-addcaches them per session.
Step 2 — upload each public key to its account
- Copy the key:
cat ~/.ssh/id_ed25519-work.pub - As account A: GitHub → Settings → SSH and GPG keys → New SSH key → paste → title it e.g. "work-laptop".
- Sign out; repeat for account B with
id_ed25519-personal.pub. - Never upload the same
.pubfile twice — GitHub errors with "Key already in use", and that error is a feature: one key, one identity.
Step 3 — teach SSH which key belongs to which host
# ~/.ssh/config
Host github-work
HostName github.com
User git
IdentityFile ~/.ssh/id_ed25519-work
IdentitiesOnly yes
Host github-personal
HostName github.com
User git
IdentityFile ~/.ssh/id_ed25519-personal
IdentitiesOnly yes
The alias names (github-work) are what you type instead of github.com in URLs. IdentitiesOnly yes is the safety pin: without it, ssh-agent may offer every loaded key and GitHub can see the wrong one first.
Step 4 — test both accounts
ssh -T git@github-work
# → Hi work-username! You've successfully authenticated...
ssh -T git@github-personal
# → Hi personal-username! You've successfully authenticated...
The greeting names the account — that's your proof the mapping works before any repository is involved.
Step 5 — clone with the alias
git clone git@github-work:company/api-server.git
git clone git@github-personal:personal-blog/site.git
Existing HTTPS repositories switch with:
git remote set-url origin git@github-work:company/api-server.git
Commit authorship is a separate topic (identity config) — SSH only fixes who pushes. See Git Identity vs Authentication.
Common beginner errors
| Symptom | Cause | Fix |
|---|---|---|
Permission denied (publickey) | Wrong/no key offered | Add -v to ssh -T; check IdentityFile path; ssh-add if using agent |
| "Key already in use" | Same public key on two accounts | Generate a second pair; one key = one account, always |
| Greeting shows the wrong account | Agent offering another key first | IdentitiesOnly yes on every host block |
| Works in Git Bash, fails elsewhere | Tool using Windows OpenSSH with different config | Point the tool at Git's SSH, or mirror the config in %USERPROFILE%\.ssh\config |
| Push uses key but commit shows wrong author | Authentication ≠ identity | Set user.email per repository |
Or let a client own the key layer
SSH multi-account is a one-time setup, but it's still you maintaining keys, aliases, and URLs. GITmaxed takes the OAuth route instead: no SSH keys to juggle — one browser login per account, tokens stored per account, each repository bound to its account for pushes and PRs. If SSH was never your preference, that's the whole alternative; if you're mid-setup, the table above resolves most errors.
Related questions answered (the same queries, one by one)
Can I use the same SSH key for two GitHub accounts?
No — GitHub rejects the same public key with "Key already in use". Generate one key pair per account.
How do I use two SSH keys with GitHub?
Store them as separate files, add each public key to its account, then define two Host aliases in ~/.ssh/config with IdentityFile and IdentitiesOnly yes; clone via the aliases.
What does IdentitiesOnly yes do?
Forces SSH to use only the key(s) listed for that host, preventing ssh-agent from offering the wrong account's key first.
How do I know which GitHub account an SSH key belongs to?
Run ssh -T git@github.com (or your alias) — the greeting says "Hi username!". GitHub also lists each key under Settings → SSH keys.
Where is the SSH config file on Windows?
In Git Bash: ~/.ssh/config (i.e. C:\Users\YOU\.ssh\config). Windows OpenSSH reads the same location.
Do I need SSH if I use a Git GUI?
No — GUIs like GITmaxed authenticate via OAuth tokens per account, so multi-account works without any SSH setup.
No comments:
Post a Comment