This setup uses some tricks to ensure that the right email/name/ssh-key is used for the right repos without having to think about it ever again.
- First generate two SSH keys,
~/.ssh/id_ed25519
and~/.ssh/id_ed25519_work
- Add one key to your personal account and the other to your work account
# Personal SSH Key
Host github.com
HostName github.com
User git
IdentityFile ~/.ssh/id_ed25519
# Work SSH Key: Rewrite `github.com-work` host to `github.com`
Host github.com-work
HostName github.com
User git
IdentityFile ~/.ssh/id_ed25519_work
Test with
ssh -T [email protected]
andssh -T [email protected]
# Personal Git Config
[user]
email = [email protected]
name = Yours Truly
# Assuming all your work repositories are in a single github org like `github.com/WORK_ORG`
# Conditionally include configs to override settings in those repositories
[includeIf "hasconfig:remote.*.url:[email protected]:WORK_ORG/**"]
path = .gitconfig-work
# Rewrite `github.com:WORK_ORG` to `github.com-work:WORK_ORG` to get the right SSH key
[url "[email protected]:WORK_ORG/"]
insteadOf = [email protected]:WORK_ORG/
Test by cloning a personal repo and a work repo using the SSH URL (
[email protected]
)
# You can override any settings here you want for your work org
[user]
email = [email protected]
name = Yours Truly
Test by running
git config user.email
in personal and work repositories