Configuring a git client for multiple git accounts with SSH keys
Created by brian on
I've been using GitKraken for a long time and I'm used to the profiles feature handling my work and personal identities. But I'm trying to switch to something lighter like Sublime Merge or Fork, and I hit a wall on how to manage multiple accounts without those profiles.
The fix is using the SSH config file. You can set up aliases to tell your git client which key to use for which repo. This lets you push and pull from different accounts on the same machine.
Here's how I set it up.
The goal
Configure two accounts:
- Personal (e.g.
personal-user) - Work (e.g.
work-user)
Step 1: Generate unique SSH keys
Open a terminal. We need a separate key pair for each account.
bash# go to .ssh directory cd ~/.ssh # generate key for personal account ssh-keygen -t ed25519 -C "personal@email.com" -f id_ed25519_personal # generate key for work account ssh-keygen -t ed25519 -C "work@company.com" -f id_ed25519_work
Now you have four files in ~/.ssh:
id_ed25519_personal(private)id_ed25519_personal.pub(public)id_ed25519_work(private)id_ed25519_work.pub(public)
Step 2: Add keys to GitHub
Tell GitHub about these keys.
- Copy the personal public key:
bashpbcopy < ~/.ssh/id_ed25519_personal.pub
- Log in to your personal GitHub account.
- Go to Settings > SSH and GPG keys > New SSH key.
- Paste the key and give it a name like "MacBook Personal".
Do the same thing for your work account using the id_ed25519_work.pub key.
Step 3: Configure the SSH config file
This is the main trick. Create a config file that maps a fake hostname to the right key.
Edit or create the config file:
bashnano ~/.ssh/config
Add this config:
ssh# personal account - default Host github.com HostName github.com User git IdentityFile ~/.ssh/id_ed25519_personal # work account - alias Host github-work HostName github.com User git IdentityFile ~/.ssh/id_ed25519_work
What this does:
- Connecting to
github.comuses your personal key. - Connecting to
github-workconnects to the realgithub.combut uses your work key.
Step 4: Cloning repositories
When cloning, just change the domain name for work projects.
For personal projects (normal):
bashgit clone git@github.com:personal-user/my-project.git
For work projects (using the alias):
bashgit clone git@github-work:company-org/work-project.git
Just replace github.com with github-work in the URL.
Step 5: Configure local git identity
SSH handles the login, but git still needs to know who is making the commits.
For personal projects, set a global default:
bashgit config --global user.name "Personal Name" git config --global user.email "personal@email.com"
For work repos, override this locally. Go to the cloned work repo and run:
bashcd work-project git config user.name "Work Name" git config user.email "work@company.com"
Pro tip: conditional includes
If you keep all work projects in one folder like ~/Work/, you can automate the email config.
Add this to your global ~/.gitconfig:
ini[includeIf "gitdir:~/Work/"] path = ~/.gitconfig-work
Then create ~/.gitconfig-work:
ini[user] email = work@company.com name = Work Name
Conclusion
Using SSH config aliases separates your identity management from the git client. Whether using Sublime Merge, Fork, Tower, or the CLI, this setup makes sure you authenticate as the right user without switching profiles.
Last updated