How to Access Both Company and Personal GitHub Repositories On The Same Machine
Source: Dev.to
Introduction
Managing multiple GitHub accounts—personal and company—can be tricky, especially when you need to keep repositories separate and avoid committing to the wrong account. Using distinct SSH keys for each account simplifies the workflow and improves security.
Step 1: Generate Two SSH Key Pairs
Create a dedicated RSA key pair for each GitHub account.
Company account
ssh-keygen -t rsa -b 4096 -C "your-company-email@example.com" -f ~/.ssh/id_rsa_company-Cadds a comment (usually your email) to identify the key.-fspecifies the filename so the default key isn’t overwritten.
You’ll be prompted to set a passphrase—highly recommended.
The command creates:
~/.ssh/id_rsa_company(private key)~/.ssh/id_rsa_company.pub(public key)
Personal account
ssh-keygen -t rsa -b 4096 -C "your-personal-email@example.com" -f ~/.ssh/id_rsa_personalThis creates:
~/.ssh/id_rsa_personal(private key)~/.ssh/id_rsa_personal.pub(public key)
Step 2: Configure SSH to Use the Right Key
Edit (or create) ~/.ssh/config and define host aliases for each account.
vi ~/.ssh/configAdd the following:
# Company GitHub Account
Host github-company
HostName github.com
User git
IdentityFile ~/.ssh/id_rsa_company
IdentitiesOnly yes
# Personal GitHub Account
Host github-personal
HostName github.com
User git
IdentityFile ~/.ssh/id_rsa_personal
IdentitiesOnly yesSave and close the file. You can add more sections if you manage additional accounts.
Step 3: Add Public Keys to Your GitHub Accounts
Company account
cat ~/.ssh/id_rsa_company.pubCopy the entire output (starting with ssh-rsa and ending with your email).
- Go to Settings → SSH and GPG keys in your company GitHub account.
- Click New SSH key, give it a title, paste the key, and save.
Personal account
cat ~/.ssh/id_rsa_personal.pubRepeat the same steps in your personal GitHub account to add the key.
Cloning Repositories with the Correct Host
When cloning, use the host alias you defined in ~/.ssh/config.
Personal repository
git clone git@github-personal:USERNAME/REPO.gitCompany repository
git clone git@github-company:USERNAME/REPO.gitReplace USERNAME/REPO with the appropriate repository path.
Setting a Remote for an Existing Local Repository
To push to the correct account, add a remote that uses the corresponding host alias.
Personal remote
git remote add origin git@github-personal:USERNAME/REPO.gitCompany remote
git remote add origin git@github-company:USERNAME/REPO.gitNow git push and git pull will automatically use the right SSH key.
Quick Reference
| Alias | Host in ~/.ssh/config | Key file |
|---|---|---|
github-personal | github.com | ~/.ssh/id_rsa_personal |
github-company | github.com | ~/.ssh/id_rsa_company |
With these steps, you can seamlessly work with both personal and company GitHub repositories on the same machine.