
When working with both personal and organizational GitHub accounts, you may run into conflicts if you try to use them on the same machine with a single SSH key. This guide explains how to configure multiple SSH keys, set up Git to manage identities, and streamline your workflow with conditional configurations.
Many developers maintain a personal GitHub account while also working on projects under an organization. Without proper setup, you might:
To solve this, we configure separate SSH keys and use Git’s conditional configuration to switch identities automatically.
Each GitHub account should have its own SSH key.
ssh-keygen -t ed25519 -C "personal-email@example.com" -f ~/.ssh/id_ed25519_personal
ssh-keygen -t ed25519 -C "org-email@example.com" -f ~/.ssh/id_ed25519_org
This creates two sets of keys:
~/.ssh/id_ed25519_personal and ~/.ssh/id_ed25519_personal.pub~/.ssh/id_ed25519_org and ~/.ssh/id_ed25519_org.pubLoad both keys into the SSH agent:
eval "$(ssh-agent -s)"
ssh-add ~/.ssh/id_ed25519_personal
ssh-add ~/.ssh/id_ed25519_org
Edit or create the SSH configuration file:
nano ~/.ssh/config
Add the following:
# Personal GitHub
Host github.com-personal
HostName github.com
User git
IdentityFile ~/.ssh/id_ed25519_personal
IdentitiesOnly yes
# Organization GitHub
Host github.com-org
HostName github.com
User git
IdentityFile ~/.ssh/id_ed25519_org
IdentitiesOnly yes
With this setup, you can now use github.com-personal and github.com-org as host aliases.
Copy each public key:
cat ~/.ssh/id_ed25519_personal.pub
cat ~/.ssh/id_ed25519_org.pub
When cloning repositories, specify the correct host alias:
Personal account:
git clone git@github.com-personal:YourUsername/your-repo.git
Organization account:
git clone git@github.com-org:OrgName/your-repo.git
Git requires a user identity for commits. You can set a global identity:
git config --global user.name "Your Org Name"
git config --global user.email "org-email@example.com"
This applies everywhere unless overridden locally.
If you want Git to automatically switch identities based on the repo folder, use conditional includes.
Edit the global Git configuration:
nano ~/.gitconfig
Set your personal identity as the default:
[user]
name = Your Personal Name
email = personal-email@example.com
Add a conditional include for organization repos:
[includeIf "gitdir:~/work/org/"]
path = .gitconfig-org
Create a new file ~/.gitconfig-org:
[user]
name = Your Org Name
email = org-email@example.com
Now any repository inside ~/work/org/ will use your organization identity automatically.
If a repository was cloned with the default host, update it:
git remote set-url origin git@github.com-org:OrgName/repo.git
Test SSH connections:
ssh -T git@github.com-personal
ssh -T git@github.com-org
GitHub should greet you with the correct account name.
By setting up separate SSH keys, customizing your SSH config, and using Git’s conditional configuration, you can seamlessly manage multiple GitHub accounts on a single machine. This approach ensures that: