How to setup multiple github account ssh config

If you use both a personal and a work GitHub account, you’ve likely run into issues like commits using the wrong email or pushes failing due to incorrect SSH keys.

The Problem

GitHub associates each SSH key with a single account. That means:

  • One key = one account
  • Multiple accounts = multiple SSH keys

You also need to ensure commits use the correct user.name and user.email based on the project.

Overview

Here’s what we are going to setup:

  1. Generate an SSH Key per Github account.
  2. Add each public key to the corresponding Github account.
  3. Configure ~/.ssh/config file with host aliases.
  4. Configure git to auto-switch identity based on repository directory

Below are two example accounts:

PersonalWork
GitHub userSimonSimon-work
Emailsimon@personal.devsimone@work.com
SSH key~/.ssh/id_ed25519_personal~/.ssh/id_ed25519_work
Projects dir~/personal/~/work/

Step 1: Generate SSH Keys

1
2
3
4
5
# Personal account
ssh-keygen -t ed25519 -C "simon@personal.dev" -f ~/.ssh/id_ed25519_personal

# Work account
ssh-keygen -t ed25519 -C "simon@work.com" -f ~/.ssh/id_ed25519_work

After this the output structure would like:

1
2
3
4
5
~/.ssh/
├── id_ed25519_personal       # private key (personal)
├── id_ed25519_personal.pub   # public key (personal)
├── id_ed25519_work            # private key (work)
└── id_ed25519_work.pub        # public key (work)

Step 2: Add Keys to the SSH Agent

1
2
3
4
5
6
7
8
9
# Start the agent
eval "$(ssh-agent -s)"

# Add both keys
ssh-add ~/.ssh/id_ed25519_personal
ssh-add ~/.ssh/id_ed25519_work

# Verify they're loaded (optional)
ssh-add -l

Step 3: Add public key to Github

1
2
3
4
5
6
7
# Copy personal public key
cat ~/.ssh/id_ed25519_personal.pub
# → Copy the output

# Copy work public key
cat ~/.ssh/id_ed25519_work.pub
# → Copy the output

Step 4: Configure SSH config

The core of the whole setup. Edit ~/.ssh/config:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
# Personal
Host gh-personal # This is the alias for personal account's github domain
  HostName github.com
  User git # Github use git as user to authenticate into github using ssh (git@domain.com)
  IdentiyFile ~/.ssh/id_ed25519_personal
  IdentitiesOnly yes

# Work
Host gh-work # This is the alias for work account's github domain
  HostName github.com
  User git
  IdentiyFile ~/.ssh/id_ed25519_work
  IdentitiesOnly yes
  • Host <aliases> - create a sematic alia for specific account.
  • IdentitiesOnly yes - tell ssh to only use the specified key and do not try other key.

Step 5: Verify the setup

1
2
3
4
5
# personal
ssh -T gh-personal

# work
ssh -T gh-work

Step 6: Setup remote url

For existing repos, update the remote URL:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
# personal
cd ~/personal/repo_personal
git remote set-url origin git@gh-personal:simon/repo_personal.git

# work
cd ~/work/repo_work
git remote set-url origin git@gh-work:simon-work/repo_work.git

# after setup url, verify with git command
git remote -v

References