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:

  1. Personal (e.g. personal-user)
  2. 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.

  1. Copy the personal public key:
bash
pbcopy < ~/.ssh/id_ed25519_personal.pub
  1. Log in to your personal GitHub account.
  2. Go to Settings > SSH and GPG keys > New SSH key.
  3. 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:

bash
nano ~/.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.com uses your personal key.
  • Connecting to github-work connects to the real github.com but uses your work key.

Step 4: Cloning repositories

When cloning, just change the domain name for work projects.

For personal projects (normal):

bash
git clone git@github.com:personal-user/my-project.git

For work projects (using the alias):

bash
git 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:

bash
git 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:

bash
cd 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

>

Comments (0)

Loading...

Join the conversation!

Sign up for free to leave a comment.