How to Use Two Different Git Accounts with SSH on the Same Machine
GitHub

How to Use Two Different Git Accounts with SSH on the Same Machine


In this quickly guide, we’ll cover:

  • Creating and configuring multiple SSH keys
  • Setting up Git to use different user accounts per repository
  • Automatically signing commits with the correct GPG key

1. Generate SSH Keys for Each Account

Assuming you have no conflicting keys yet, start by generating one for each GitHub account:

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

# Personal account
ssh-keygen -t ed25519 -C "you@personal.com" -f ~/.ssh/id_ed25519_personal

This creates two key pairs:

  • ~/.ssh/id_ed25519_wor and ~/.ssh/id_ed25519_work.pubk
  • ~/.ssh/id_ed25519_persona and ~/.ssh/id_ed25519_personal.publ

2. Add SSH Keys to the SSH Agent

Start the agent and add both keys:

eval "$(ssh-agent -s)"

ssh-add ~/.ssh/id_ed25519_work
ssh-add ~/.ssh/id_ed25519_personal

3. Configure the ~/.ssh/config File

# ~/.ssh/config

# Work GitHub
Host github.com-work                     # identifier
  HostName github.com                    # must match the profile host
  User git                               # git is standard user
  IdentityFile ~/.ssh/id_ed25519_work    # your ssh key for this profile

# Personal GitHub
Host github.com-personal
  HostName github.com
  User git
  IdentityFile ~/.ssh/id_ed25519_personal

This lets you clone like this:

# Personal project
git clone git@github.com-personal:username/personal-repo.git

# Work project
git clone git@github.com-work:company/work-repo.git

4. Configure Git User Info Per Repo

In each cloned repository, configure the local Git identity:

# Inside work repo
git config user.name "Work Name"
git config user.email "you@work.com"

# Inside personal repo
git config user.name "Personal Name"
git config user.email "you@personal.com"

Optionally, if you want Git to automatically pick identities based on folder paths, use a global include with conditions:

# ~/.gitconfig

[includeIf "gitdir:~/code/work/"]
  path = ~/.gitconfig-work

[includeIf "gitdir:~/code/personal/"]
  path = ~/.gitconfig-personal

Then in ~/.gitconfig-work:

[user]
  name = Work Name
  email = you@work.com
  signingkey = <your-signing-key>

Done!