Use Multiple GitHub Accounts on One Machine
Use separate SSH identities and Git configuration so personal and work repositories select the right account predictably.
1 — Create a separate SSH key
ssh-keygen -t ed25519 -C "personal@example.com" -f ~/.ssh/id_ed25519_personalAdd the generated .pub key to the corresponding GitHub account. Keep the private key private.
2 — Configure SSH host aliases
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 yesThe aliases github-work and github-personal both connect to GitHub, but each selects a different identity. IdentitiesOnly yes prevents SSH from offering unrelated agent identities before the configured key.
3 — Test authentication before cloning
ssh -T git@github-work
ssh -T git@github-personalConfirm each alias authenticates as the account you expect before changing repository remotes.
4 — Clone with the correct alias
git clone git@github-personal:YOUR-USER/YOUR-REPO.gitFor an existing repository, inspect and update the remote if necessary:
git remote -v
git remote set-url origin git@github-personal:YOUR-USER/YOUR-REPO.git5 — Set commit identity by directory
Git supports conditional includes based on the repository directory. For example, in ~/.gitconfig:
[user]
name = Work Name
email = work@example.com
[includeIf "gitdir:~/Projects/personal/"]
path = ~/.gitconfig-personalThen create ~/.gitconfig-personal:
[user]
name = Personal Name
email = personal@example.comA gitdir: pattern ending in / applies recursively beneath that directory.
6 — Verify inside a repository
git config user.name
git config user.email
git remote -v
ssh -G github-personal | grep -E "hostname|identityfile|identitiesonly"Verify both pieces: the commit identity and the SSH remote/identity selection.
Common mistakes
- Changing
user.emailand assuming that changes SSH authentication. - Creating two keys but cloning every repository through plain
github.com. - Forgetting to update an existing repository remote.
- Letting ssh-agent offer many keys without controlling identity selection.