Managing multiple GitHub accounts (like personal and work) on the same machine can be tricky if not set up properly. This guide walks you through setting up separate SSH keys, configuring Git, and switching seamlessly between accounts.
✅ Prerequisites
Before diving in, make sure you have:
Git installed on your system. (
git --version
)A GitHub account (or two).
- Basic knowledge of command line usage.
๐️ Step 1: Generate SSH Keys for Each Account
We'll generate separate SSH keys for your personal and work accounts.
# Personal GitHub account
ssh-keygen -t rsa -b 4096 -C "you@example.com" -f ~/.ssh/id_rsa_personal
# Work GitHub account
ssh-keygen -t rsa -b 4096 -C "you@work.com" -f ~/.ssh/id_rsa_work
This creates two key pairs:
~/.ssh/id_rsa_personal
&~/.ssh/id_rsa_personal.pub
~/.ssh/id_rsa_work
&~/.ssh/id_rsa_work.pub
๐ Step 2: Add SSH Keys to GitHub
Go to GitHub → Settings → SSH and GPG keys → New SSH key
Add:
For personal:
id_rsa_personal.pub
For work:
id_rsa_work.pub
Use a meaningful title for each key (e.g., “Personal Laptop”, “Work Laptop”).
๐งผ Step 3: Adding SSH Keys to the SSH agent
To add the newly generated SSH key to the SSH agent, run these commands in your terminal:
eval "$(ssh-agent -s)"
ssh-add ~/.ssh/id_rsa_personal
ssh-add ~/.ssh/id_rsa_work
Repeat these steps for each GitHub account you want to add, remembering to replace the file paths with the unique names you chose earlier.
๐ ️ Step 4: Create or Edit SSH Config
Open (or create) your SSH config file:
nano ~/.ssh/config
Add the following configuration:
# Personal GitHub account
Host github.com-personal
HostName github.com
User git
IdentityFile ~/.ssh/id_rsa_personal
# Work GitHub account
Host github.com-work
HostName github.com
User git
IdentityFile ~/.ssh/id_rsa_work
๐งช Step 5: Test SSH Connections
ssh -T git@github.com-personal
ssh -T git@github.com-work
You should see:
Hi <username>! You've successfully authenticated…
for both.
๐ Step 6: Clone Repositories with Correct Host
When cloning, make sure you use the correct Host
alias:
# Personal repo
git clone git@github.com-personal:your-username/repo-name.git# Work repogit clone git@github.com-work:work-username/repo-name.git
๐ง Step 7: Set Git Identity Per Project
๐ง Conclusion
Managing multiple GitHub accounts on the same machine is totally doable with a little SSH and Git config magic. By separating your keys and using SSH aliases, you can avoid headaches and context-switch seamlessly.
Now you can:
✅ Work with both personal and professional repos
✅ Keep identities clean and separate
✅ Avoid constant re-authentication or mistakes
0 comments:
Post a Comment