DEV Community

Akhil
Akhil

Posted on

Managing Multiple GitHub Accounts (Work + Personal) on the Same Machine

Managing Multiple GitHub Accounts

As developers, it’s common to juggle two GitHub accounts — one for work (e.g., akhil@<COMPANY_NAME>.com) and one for personal projects (e.g., akhil@gmail.com).

The challenge?

  • Git commits end up with the wrong email.
  • git push gets rejected because the wrong GitHub account is used.
  • Constantly logging in/out is frustrating.

In this post, I’ll walk you through how to configure Git + SSH to cleanly separate accounts so that your work and personal projects never interfere with each other.


🔎 The Problem I Faced

On my work laptop:

  • Git was configured with my work email globally.
  • My repos were cloned using HTTPS, which always authenticated via my company GitHub account.

When I tried pushing code to my personal GitHub repo, I got this error:

remote: Permission to d-akhil-kumar/log-panda.git denied to akhil-kumar-company-git.
fatal: unable to access 'https://github.com/d-akhil-kumar/log-panda.git/': The requested URL returned error: 403
Enter fullscreen mode Exit fullscreen mode

GitHub was picking the wrong account (akhil-kumar-company-git from work), while I needed my personal account (d-akhil-kumar).


✅ The Solution: Separate SSH Keys and Git Configs

1. Generate a Personal SSH Key

I already had a company (default) SSH key (~/.ssh/id_rsa), so I created a new one for my personal account:

ssh-keygen -t ed25519 -C "your-personal-email@example.com" -f ~/.ssh/id_ed25519_personal
Enter fullscreen mode Exit fullscreen mode

Now I have:

  • ~/.ssh/id_rsa → company key
  • ~/.ssh/id_ed25519_personal → personal key

2. Add Keys to the SSH Agent

eval "$(ssh-agent -s)"
ssh-add ~/.ssh/id_rsa              # company
ssh-add ~/.ssh/id_ed25519_personal # personal
Enter fullscreen mode Exit fullscreen mode

3. Add the Public Key to GitHub

Copy the personal key:

cat ~/.ssh/id_ed25519_personal.pub
Enter fullscreen mode Exit fullscreen mode

Go to GitHub → Settings → SSH and GPG keys → New SSH key and paste it.


4. Configure SSH for Multiple Accounts

Edit (or create) ~/.ssh/config:

# Company GitHub
Host github.com-company
    HostName github.com
    User git
    IdentityFile ~/.ssh/id_rsa

# Personal GitHub
Host github.com-personal
    HostName github.com
    User git
    IdentityFile ~/.ssh/id_ed25519_personal
Enter fullscreen mode Exit fullscreen mode

This tells SSH which key to use based on the alias (github.com-company or github.com-personal).


5. Update Git Remotes

Inside a personal repo, update the remote URL:

git remote set-url origin git@github.com-personal:your-username/your-repo.git
Enter fullscreen mode Exit fullscreen mode

For a company repo:

git remote set-url origin git@github.com-company:company-org/repo-name.git
Enter fullscreen mode Exit fullscreen mode

Check with:

git remote -v
Enter fullscreen mode Exit fullscreen mode

6. Test the Connection

For personal:

ssh -T git@github.com-personal
Enter fullscreen mode Exit fullscreen mode

For company:

ssh -T git@github.com-company
Enter fullscreen mode Exit fullscreen mode

Both should greet you with the correct account username 🎉


7. Set Git Commit Identity Per Repo

Inside a personal repo:

git config user.name "Your Name"
git config user.email "your-personal-email@example.com"
Enter fullscreen mode Exit fullscreen mode

Inside a company repo:

git config user.name "Your Name"
git config user.email "your-work-email@<COMPANY_NAME>.com"
Enter fullscreen mode Exit fullscreen mode

Now, commits show up under the right account.


⚡ Bonus: Auto-Switch Identities

If you keep repos in separate folders (~/work/ and ~/personal/), Git can auto-switch your email:

Edit ~/.gitconfig:

[user]
    name = Your Name
    email = your-personal-email@example.com

[includeIf "gitdir:~/work/"]
    path = ~/.gitconfig-company
Enter fullscreen mode Exit fullscreen mode

Create ~/.gitconfig-company:

[user]
    name = Your Name
    email = your-work-email@<COMPANY_NAME>.com
Enter fullscreen mode Exit fullscreen mode

Now you never need to run git config manually again. 🚀


🎯 Final Result

  • Personal repos → push with personal GitHub account and personal email
  • Company repos → push with company GitHub account and company email
  • No more 403 errors or wrong commit authorship

🙌 Conclusion

Managing multiple GitHub accounts doesn’t have to be painful.
With separate SSH keys and smart Git config, you can seamlessly switch between personal and work repos without conflicts.


💡 Have you set up multiple GitHub accounts on your machine? Did you use SSH, HTTPS, or another trick? Let me know in the comments!

Top comments (0)