Authenticate GitHub Accounts Locally Using SSH
by iamvkr on
Authenticate to GitHub Accounts Locally Using SSH (Git Bash Guide)
If you’re a developer juggling:
- A primary/personal GitHub account
- A work/organizational GitHub account
You’ve probably faced this problem:
How do I switch GitHub accounts locally without constantly logging in and out?
If you’re using HTTPS, you may find yourself clearing credentials, logging out, logging back in, and repeating the cycle. That quickly becomes painful.
This guide shows the proper, scalable solution using SSH keys using Git Bash.
The Problem
When using Git locally, two separate things matter:
- Commit Identity (user.name and user.email)
- Authentication (which GitHub account you push to)
Even if you change your email locally:
git config user.email "work@email.com"
You might still push using the wrong GitHub account if authentication is shared.
Logging out and logging back in via HTTPS works — but it’s not efficient.
The Clean Solution: Separate SSH Keys Per Account
Instead of switching accounts, we configure Git so:
- Personal projects repos use your primary GitHub account
- Work repo use your Organizational GitHub account
- No login/logout needed
This works by:
- Generating separate SSH keys
- Mapping them via an SSH config file
- Cloning repositories using custom host aliases
Platform: Git Bash (Works on Windows, macOS, Linux)
This guide assumes:
- You’re using Git Bash
- Git is installed
- OpenSSH is available (comes with Git)
Git Bash uses Unix-style paths, which makes it consistent across environments.
🔐 Step 1: Generate SSH Keys (One Per Account)
In Git Bash:
ssh-keygen -t ed25519 -C "primary@email.com"
When prompted:
Enter file in which to save the key:
Instead of pressing Enter, type:
~/.ssh/id_ed25519_primary
Repeat for your second account:
ssh-keygen -t ed25519 -C "workOrganizational@email.com"
Save as:
~/.ssh/id_ed25519_work
Now your keys are correctly stored inside the .ssh directory.
🌐 Step 2: Add Public Keys to GitHub
Open your GitHub accounts:
- Primary account → Settings → SSH and GPG keys → Add new key
- Organizational account → Same process
Copy each public key:
cat ~/.ssh/id_ed25519_primary.pub
(notice the .pub => it means it’s a public key and can be shared to public)
Paste it into the correct account.
⚙️ Step 3: Create SSH Config File
Create or edit:
nano ~/.ssh/config
Add:
# Primary GitHub Account
Host github-primary
HostName github.com
User git
IdentityFile ~/.ssh/id_ed25519_primary
# Side GitHub Account
Host github-work
HostName github.com
User git
IdentityFile ~/.ssh/id_ed25519_work
This tells SSH:
- When connecting to
github-primary, use primary key - When connecting to
github-work, use side key
Save and exit.
🧪 Step 4: Test Authentication
Test your primary account:
ssh -T git@github-primary
If successful, you’ll see:
Hi yourusername! You’ve successfully authenticated, but GitHub does not provide shell access.
Repeat for work account:
ssh -T git@github-work
If both work — you’re done 🎉
📦 Step 5: Clone Repositories Using Correct Alias
Instead of cloning like this:
git@github.com:username/repo.git
Clone using your alias:
Primary account:
git clone git@github-primary:username/repo.git
Side account:
git clone git@github-work:username/repo.git
Now each repository permanently uses the correct GitHub account.
No switching needed.
🧾 Step 6: Set Commit Identity Per Repository (Important)
Inside each repo:
For primary:
git config user.name "Your Name"
git config user.email "primary@email.com"
For work projects:
git config user.name "Your Name"
git config user.email "workOrganizational@email.com"
This ensures commits are correctly attributed.
🔍 How to Check Which Account You’re Using
Check commit identity:
git config user.name
git config user.email
Check SSH authentication:
ssh -T git@github-primary
ssh -T git@github-work
❌ Why HTTPS Login/Logout Is Not Ideal
With HTTPS, you must:
- Clear stored credentials
- Log in again
- Repeat when switching accounts
This is:
- Manual
- Error-prone
- Not scalable
SSH avoids all of that.
🏆 Best Practice Summary
| Method | Recommended? | Reason |
|---|---|---|
| Separate SSH keys | ✅ Yes | Automatic & clean |
| Per-repo git config | ✅ Yes | Correct commit attribution |
| HTTPS login/logout | ❌ No | Manual & repetitive |
🎯 Final Result
You now have:
- Two GitHub accounts
- Two SSH keys
- One machine
- Zero login/logout frustration
This is the professional way to manage multiple GitHub identities locally.
If you’re serious about clean dev workflows, SSH-based multi-account setup is absolutely worth it.