If you've ever struggled to juggle your personal and work GitHub accounts on the same machine, this guide is for you.
In this step-by-step tutorial, you'll learn how to:
β
Generate SSH keys for different GitHub accounts
β
Configure SSH with aliases for each identity
β
Clone repositories using SSH without constant authentication issues
Letβs dive in π
1. Generate SSH Keys for Personal and Work GitHub Accounts
Start by creating two separate SSH keysβone for each GitHub account.
# Personal GitHub account
ssh-keygen -t ed25519 -C "[email protected]" -f ~/.ssh/id_ed25519
# Work GitHub account
ssh-keygen -t rsa -b 4096 -C "[email protected]" -f ~/.ssh/id_rsa_work
π Notes:
- The
-f
flag specifies the filename. - Use meaningful filenames like
id_ed25519
andid_rsa_work
for clarity.
2. Add Your Public Keys to GitHub
Copy the contents of each public key and add them to their respective GitHub accounts:
# Display the public key
cat ~/.ssh/id_ed25519.pub # Personal
cat ~/.ssh/id_rsa_work.pub # Work
Then:
- Go to your GitHub Settings > SSH and GPG keys
- Click "New SSH Key"
- Paste the key content and save
Repeat this process for both personal and work accounts.
3. Configure the SSH Config File for Multiple Identities
Create or update your ~/.ssh/config
file to define aliases for each account:
# Personal GitHub
Host github-personal
HostName github.com
User git
IdentityFile ~/.ssh/id_ed25519
# Work GitHub
Host github-work
HostName github.com
User git
IdentityFile ~/.ssh/id_rsa_work
π οΈ You can edit this file using your favorite text editor:
code ~/.ssh/config # VS Code
nano ~/.ssh/config # Terminal editor
4. Test Your SSH Connections
Verify your setup with:
ssh -T git@github-personal
ssh -T git@github-work
β
You should see:
Hi your-username! You've successfully authenticated, but GitHub does not provide shell access.
5. Clone Repositories Using SSH Aliases
Now you can clone repos with the correct identity by using the aliases:
# Personal repo
git clone git@github-personal:your_personal_username/repo.git
# Work repo
git clone git@github-work:your_work_org/repo.git
No more switching keys or editing global settings!
6. Set Local Git User Info for Work Repositories
To ensure your work commits use the correct email and name:
cd path/to/work-repo
git config user.name "Your Work Name"
git config user.email "[email protected]"
This updates the local .git/config
file and overrides the global config.
π Check your Git identity:
git config user.name
git config user.email
π You're All Set!
With this setup, you can seamlessly manage multiple GitHub accounts on one machine using SSH. It's a clean and secure way to avoid authentication conflicts and commit with the correct identity every time.
π TL;DR
- Use different SSH keys for personal and work accounts
- Define aliases in
~/.ssh/config
- Clone repos using the correct alias
- Set local Git user info per project
π¬ Got questions or a better SSH setup? Connect on LinkedIn
Top comments (0)