Stop Pushing Work Code with Your Personal Email: The Ultimate Guide to Multiple GitHub Accounts on Windows
Source: Dev.to
The Concept: SSH Keys & Aliases
Git uses a single global user by default. To switch between accounts you need:
- Two separate SSH keys (one for personal, one for work).
- An SSH config file that maps an alias (e.g.,
github-work) to the appropriate key.
Step 1: Generate Unique SSH Keys
# Create the .ssh directory if it doesn't exist
mkdir "$HOME\.ssh"
cd "$HOME\.ssh"
Personal key
ssh-keygen -t ed25519 -C "personal@gmail.com" -f id_personal
Work key
ssh-keygen -t ed25519 -C "work@company.com" -f id_work
Note: Do not overwrite your existing default key (
id_rsa).
Step 2: The “Eval” Problem (Windows Solution)
The usual eval $(ssh-agent -s) command often fails in PowerShell. Instead, enable the built‑in Windows ssh-agent service so it persists across reboots.
# Run PowerShell as Administrator
Get-Service -Name ssh-agent | Set-Service -StartupType Automatic
Start-Service ssh-agent
Add your keys (you can do this in a regular PowerShell window):
ssh-add $HOME\.ssh\id_personal
ssh-add $HOME\.ssh\id_work
Verify that both keys are loaded:
ssh-add -l
Step 3: Upload Keys to GitHub
- Open the Personal public key file (
id_personal.pub) and copy its contents. - In GitHub → Settings → SSH and GPG keys, click New SSH key, paste the key, and save.
- Repeat the process with the Work public key (
id_work.pub) on your company’s GitHub account.
Step 4: The Magic Config File
Create a file named config (no extension) inside $HOME\.ssh\ with the following content:
# Personal Account – Default
Host github.com
HostName github.com
User git
IdentityFile ~/.ssh/id_personal
# Work Account – Alias
Host github-work
HostName github.com
User git
IdentityFile ~/.ssh/id_work
Step 5: Cloning & Configuring Repositories
-
Personal projects: Clone as usual.
git clone git@github.com:username/repo.git -
Work projects: Use the alias
github-workinstead ofgithub.com.git clone git@github-work:company/repo.git
Set Your Local Identity
Immediately after cloning a work repository, configure the local Git identity to avoid committing with the wrong email:
git config user.name "Your Work Name"
git config user.email "work@company.com"
Bonus: Fixing a Commit Pushed with the Wrong Email
If you already pushed a commit that shows your personal email, you can rewrite it without deleting the repository.
-
Update the local Git config (if not already set):
git config user.email "work@company.com" -
Amend the last commit to reset the author information:
git commit --amend --reset-author --no-edit -
Force‑push the corrected commit:
git push -f origin main
Conclusion
Setting up multiple GitHub accounts on Windows takes about 10 minutes but saves you from endless “Permission denied” errors and the embarrassment of mixing identities. Once the ssh-agent service and SSH config file are in place, everything works seamlessly in the background.
Happy coding! 🚀