ssh-keygen -t ed25519 -C "personal@email.com" -f ~/.ssh/id_ed25519_personal
ssh-keygen -t ed25519 -C "work@email.com" -f ~/.ssh/id_ed25519_workCopy each .pub file content and add it under Settings > SSH and GPG keys on the corresponding GitHub account.
cat ~/.ssh/id_ed25519_personal.pub
cat ~/.ssh/id_ed25519_work.pubIn ~/.ssh/config, create a distinct host alias per account:
Host gh-personal
HostName github.com
User git
AddKeysToAgent yes
IdentitiesOnly yes
IdentityFile ~/.ssh/id_ed25519_personal
Host gh-work
HostName github.com
User git
AddKeysToAgent yes
IdentitiesOnly yes
IdentityFile ~/.ssh/id_ed25519_work
IdentitiesOnly yes is critical. Without it, the SSH agent may offer the wrong key first, causing authentication to succeed as the wrong user.
ssh-add ~/.ssh/id_ed25519_personal
ssh-add ~/.ssh/id_ed25519_workssh -T gh-personal # should print: Hi <personal-username>!
ssh -T gh-work # should print: Hi <work-username>!Instead of the standard github.com hostname, use your alias:
git clone git@gh-personal:personal-user/repo.git
git clone git@gh-work:work-org/repo.gitFor existing repos, update the remote:
git remote set-url origin git@gh-personal:personal-user/repo.gitGit author info is separate from SSH. Set it per repo to avoid committing under the wrong name:
cd ~/work-repo
git config user.name "Work Name"
git config user.email "work@email.com"Or use conditional includes in ~/.gitconfig to automate by directory:
[includeIf "gitdir:~/work/"]
path = ~/.gitconfig-workWhere ~/.gitconfig-work contains:
[user]
name = Work Name
email = work@email.com