If you contribute to both personal and professional projects, you’ve likely run into the frustrating situation where Git doesn’t know which identity or SSH key to use. Fortunately, due to the power of ssh and git, with a few config tweaks, you can cleanly separate your personal and work GitHub identities and seamlessly switch between them.
This guide walks through setting up your CLI environment on macOS (or Linux) to:
- Use separate SSH keys for personal and work GitHub accounts
- Automatically route the correct key to the correct GitHub username
- Configure Git to use the appropriate
user.name
anduser.email
per project
Step 1: Generate SSH Keys for Each Account
# Personal GitHub key
ssh-keygen -t ed25519 -C "[email protected]" -f ~/.ssh/id_ed25519
# Work GitHub key
ssh-keygen -t ed25519 -C "[email protected]" -f ~/.ssh/id_ed25519_work
Then add them to your macOS keychain:
eval "$(ssh-agent -s)"
ssh-add --apple-use-keychain ~/.ssh/id_ed25519
ssh-add --apple-use-keychain ~/.ssh/id_ed25519_work
Step 2: Add Public Keys to GitHub
Copy the contents of ~/.ssh/id_ed25519.pub
and ~/.ssh/id_ed25519_work.pub
, and add them to your respective GitHub accounts under Settings > SSH and GPG Keys.
Step 3: Configure Your SSH Client
Edit your ~/.ssh/config
to define separate host aliases for GitHub:
# Personal GitHub
Host github.com
HostName github.com
User git
IdentityFile ~/.ssh/id_ed25519
UseKeychain yes
AddKeysToAgent yes
IdentitiesOnly yes
# Work GitHub
Host github.com-work
HostName github.com
User git
IdentityFile ~/.ssh/id_ed25519_work
UseKeychain yes
AddKeysToAgent yes
IdentitiesOnly yes
The key line here is
IdentitiesOnly yes
, which ensures SSH only uses the specified identity rather than trying every key in your agent.
Step 4: Configure Git to Rewrite URLs
Git doesn’t know about SSH aliases like github.com-work
, so you have to teach it to rewrite URLs.
Add the following to your ~/.gitconfig
:
[url "[email protected]:your-work-username/"]
insteadOf = git@github.com:your-work-username/
insteadOf = https://github.com/your-work-username/
[url "[email protected]:"]
insteadOf = https://github.com/
This means:
- URLs for your work account (e.g.,
[email protected]:your-work-username/repo.git
) will be rewritten to use yourgithub.com-work
SSH alias. - Other GitHub URLs will use your personal identity.
Step 5: Set Git User Info Per Project
For your personal projects, the default name/email in ~/.gitconfig
will likely suffice:
[user]
name = Your Name
email = your-personal-email@example.com
To override Git user info for work projects, add this to ~/.gitconfig
:
[includeIf "gitdir:~/git/work/"]
path = ~/.gitconfig-work
Then create ~/.gitconfig-work
with:
[user]
name = Your Work Name
email = your-work-email@example.com
This automatically applies your work Git identity to all repos under ~/git/work/
.
Bonus: Testing
To test your setup:
# Personal GitHub
ssh -T [email protected]
# Work GitHub
ssh -T [email protected]
Both should return something like:
Hi yourusername! You've successfully authenticated...
And to confirm which key Git is using:
GIT_TRACE=1 GIT_SSH_COMMAND="ssh -v" git fetch
Look for Offering public key:
and Authenticated to github.com
lines.
Final Thoughts
With this setup, you can:
- Clone work repos as
[email protected]:your-work-username/repo.git
and they’ll auto-use your work SSH key - Clone personal repos normally and use your personal key
- Keep your commits clean with the right name/email on every repo
This makes it easy to contribute to both personal and professional projects without messing around with keys, passwords, or constantly switching Git config manually.
Happy coding! ⚡
Leave a Reply