One Machine, Multiple GitHub Identities: A Team-Friendly SSH Configuration Guide
In today's world of remote work and side projects, many developers find themselves using the same machine for both company work and personal coding. This often leads to a common challenge: how do you manage different GitHub (or other Git service) identities seamlessly and securely? Committing to a work repository with your personal account, or vice-versa, can lead to confusion, failed pushes, or even policy violations.
This guide provides a robust, team-friendly solution using SSH and Git's configuration. It allows you to set a "default" account (e.g., personal) and automatically use your work account for specific repositories, all without requiring any changes that would affect your teammates' workflows.
The Goal:
Step 1: Create Separate SSH Keys for Each Account
First, ensure you have a unique SSH key for each GitHub account.
We'll use the modern and recommended Ed25519 algorithm.
Open your terminal and run these commands, replacing the email and filenames as needed. The -f flag is crucial to specify a unique filename and avoid overwriting existing keys.
Generate a personal key: If you don't have a default SSH key yet, you can accept the default path (~/.ssh/id_ed25519) for your personal key. If you already have one, or want to be explicit:
ssh-keygen -t ed25519 -C "your_personal@email.com" -f ~/.ssh/id_ed25519_personal
Generate a work key:
ssh-keygen -t ed25519 -C "your_work@email.com" -f ~/.ssh/id_ed25519_work
When prompted for a passphrase, it's highly recommended to enter a strong one for each key.
Add the public keys to your GitHub accounts:
Step 2: Configure SSH to Use Your Keys
The magic happens in the SSH configuration file, typically located at ~/.ssh/config. This file tells your SSH client which private key to use when connecting to a specific server address (a "Host").
Create or open ~/.ssh/config with a text editor and add the following configuration:
# Personal GitHub account (DEFAULT)
# This block is for the standard github.com host
Host github.com
HostName github.com
User git
IdentityFile ~/.ssh/id_ed25519_personal # <-- Path to your PERSONAL key
IdentitiesOnly yes
AddKeysToAgent yes # Optional: Adds key to ssh-agent automatically if it's running
# Work GitHub account (SPECIAL CASE)
# We invent a custom host "github-work" to use for work repos.
Host github-work
HostName github.com
User git
IdentityFile ~/.ssh/id_ed25519_work # <-- Path to your WORK key
IdentitiesOnly yes
AddKeysToAgent yes # Optional
Step 3: Tell Git to Rewrite URLs Automatically (The Team-Friendly Part)
You could manually change your work repository remote URLs from git@github.com:YourWorkOrg/project.git to git@github-work:YourWorkOrg/project.git. However, if you commit this change (e.g., in a .gitmodules file for submodules), it will break the setup for your teammates, as their machines don't know what github-work means.
Recommended by LinkedIn
The elegant and team-friendly solution is to tell your local Git installation to rewrite these URLs on the fly, without altering the repository's actual configuration files that get committed.
Run this global Git configuration command in your terminal, replacing YourWorkOrg with your company's actual GitHub organization name (or your work username if it's a personal work repo under your work account but not in an org):
git config --global url."git@github-work:YourWorkOrg/".insteadOf "git@github.com:YourWorkOrg/"
If your work repositories are not under a specific organization but directly under your work username, the command would be:
git config --global url."git@github-work:YourWorkUsername/".insteadOf "git@github.com:YourWorkUsername/"
This command adds a section to your global ~/.gitconfig file. It instructs Git: "For this machine only, any time you encounter an SSH URL for a repository under YourWorkOrg (or YourWorkUsername) that uses github.com, silently replace the github.com part with our special github-work alias before attempting the SSH connection."
How It All Works Together
Now, when you run a command like git pull or git push in a work repository (e.g., one cloned from git@github.com:YourWorkOrg/project.git):
Your teammates' machines don't have this local insteadOf rule or the github-work SSH host alias, so they connect to the standard git@github.com:YourWorkOrg/project.git URL using their own default SSH key setup, as they always have. No conflicts, no shared pain!
Verification
You can test that your SSH configuration is correctly identifying keys for each host alias:
# This should connect using your PERSONAL key
ssh -T git@github.com
Expected output will include something like: Hi YourPersonalUsername! You've successfully authenticated...
# This should connect using your WORK key
ssh -T git@github-work
Expected output will include something like:
Hi YourWorkUsername! You've successfully authenticated...
If both commands give you the expected "Hi [CorrectUsername]!" messages, you're all set! You now have a powerful, flexible, and team-friendly system for managing multiple GitHub identities on the same machine.
Remember to replace placeholders like your_personal@email.com, YourWorkOrg, etc., with actual values.