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:
- Generate an SSH Key per Github account.
- Add each public key to the corresponding Github account.
- Configure
~/.ssh/configfile with host aliases. - Configure git to auto-switch identity based on repository directory
Below are two example accounts:
| Personal | Work | |
|---|---|---|
| GitHub user | Simon | Simon-work |
simon@personal.dev | simone@work.com | |
| SSH key | ~/.ssh/id_ed25519_personal | ~/.ssh/id_ed25519_work |
| Projects dir | ~/personal/ | ~/work/ |
Step 1: Generate SSH Keys
| |
After this the output structure would like:
| |
Step 2: Add Keys to the SSH Agent
| |
Step 3: Add public key to Github
| |
Step 4: Configure SSH config
The core of the whole setup. Edit ~/.ssh/config:
| |
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
| |
Step 6: Setup remote url
For existing repos, update the remote URL:
| |
References
- GitHub Docs: Managing multiple accounts — GitHub’s official guide on handling multiple accounts with SSH and HTTPS.
- GitHub Docs: Connecting to GitHub with SSH — Official walkthrough for generating keys and adding them to your account.
- GitHub Docs: Adding a new SSH key to your GitHub account — Step-by-step for uploading your public key.
- Git Documentation: git-config Conditional Includes — Official docs on
includeIfwithgitdir,gitdir/i,onbranch, andhasconfig. - OpenSSH Manual: ssh_config — Full reference for all SSH config options (
IdentityFile,IdentitiesOnly,Host, etc.). - Arch Wiki: SSH keys — Arch-specific guide covering key generation, ssh-agent, and systemd integration.