DEV Community

Rahul Kumar Bharti
Rahul Kumar Bharti

Posted on

Stop Pushing Work Code with Your Personal Email: The Ultimate Guide to Multiple GitHub Accounts on Windows

If you are like me, you probably have a personal GitHub account for your side projects and a separate professional account for your company. And if you’re working on a Windows machine using VS Code, you’ve likely hit the wall where Git doesn't know who you are.

I recently spent hours struggling with this. I tried running Linux commands like eval $(ssh-agent -s) in PowerShell (which didn't work), got "Permission Denied" errors, and even accidentally pushed work code using my personal email. 🤦‍♂️

After figuring it out, I decided to document the exact, battle-tested method to manage multiple GitHub accounts on Windows without going crazy.

Here is how to do it right.


The Concept: SSH Keys & Aliases

By default, Git pushes code as one global user. To switch between accounts, we need:

  1. Two separate SSH Keys (ID cards).
  2. A Config File that tells your computer: "If I use the alias github-work, use my Work Key."

Step 1: Generate Unique SSH Keys

Open PowerShell and create a .ssh directory if you don't have one.

mkdir $HOME\.ssh
cd $HOME\.ssh

Enter fullscreen mode Exit fullscreen mode

Now, create two keys. Don't overwrite your existing default key (id_rsa). Give them specific names:

For Personal:

ssh-keygen -t ed25519 -C "personal@gmail.com" -f id_personal
Enter fullscreen mode Exit fullscreen mode

For Work:

ssh-keygen -t ed25519 -C "work@company.com" -f id_work
Enter fullscreen mode Exit fullscreen mode

Step 2: The "Eval" Problem (And the Windows Solution)

Most tutorials tell you to run eval $(ssh-agent -s). This command often fails or does nothing in Windows PowerShell.

Instead of fighting with the terminal, use the native Windows Service. This is better because it persists even after you restart your computer.

  1. Open PowerShell as Administrator.
  2. Run these two commands:

# Set the service to run automatically
Get-Service -Name ssh-agent | Set-Service -StartupType Automatic

# Start the service
Start-Service ssh-agent
Enter fullscreen mode Exit fullscreen mode
  1. Now, add your keys (you can do this in a normal PowerShell window):

ssh-add $HOME\.ssh\id_personal
ssh-add $HOME\.ssh\id_work
Enter fullscreen mode Exit fullscreen mode

Run ssh-add -l to confirm both keys are loaded.

Step 3: Upload Keys to GitHub

You need to introduce yourself to GitHub.

  1. Copy the content of your Personal Public Key (id_personal.pub).
  2. Go to Personal GitHub > Settings > SSH Keys > New SSH Key and paste it.
  3. Repeat this for your Work Public Key (id_work.pub) on your Company GitHub account.

Step 4: The Magic Config File (The Router)

This is the most important step. Create a file named config (no extension) inside your $HOME\.ssh\ folder.

Paste this configuration:


# Personal Account - Default
Host github.com
  HostName github.com
  User git
  IdentityFile ~/.ssh/id_personal

# Work Account - The Custom Alias
Host github-work
  HostName github.com
  User git
  IdentityFile ~/.ssh/id_work

Enter fullscreen mode Exit fullscreen mode

Step 5: Cloning & Configuring Repos

Here is where the workflow changes slightly.
For Personal Projects: Clone as usual: git clone git@github.com:username/repo.git

For Work Projects: You must replace github.com with your alias github-work:


git clone git@github-work:company/repo.git

Enter fullscreen mode Exit fullscreen mode

⚠️ Crucial Step: Set Your Local Identity

Once you clone the work repo, immediately run this inside the folder. If you forget this, your commits will show up as your personal user!


git config user.name "Your Work Name"
git config user.email "work@company.com"

Enter fullscreen mode Exit fullscreen mode

Bonus: "Oops, I already pushed with the wrong email!"
I did this. I pushed a commit to my company repo, and it showed my personal Gmail and profile picture. Panic mode! 🚨

But you can fix it without deleting the repo.

  1. Fix your config first:

git config user.email "work@company.com"
Enter fullscreen mode Exit fullscreen mode
  1. Amend the last commit: This command rewrites the last commit with your new config.

git commit --amend --reset-author --no-edit
Enter fullscreen mode Exit fullscreen mode
  1. Force Push: Since you changed history, you need to force the update.

git push -f origin main
Enter fullscreen mode Exit fullscreen mode

And just like that, your commit history is clean!

Conclusion

Setting this up takes about 10 minutes, but it saves you from endless "Permission Denied" errors and the embarrassment of mixing up identities. Once the SSH Agent service and Config file are set, everything just works in the background.

Happy Coding! 🚀

Top comments (0)