Separate GitHub Accounts for Work and Personal Projects: A Step-by-Step Guide

Emmanuel Nkrumah-Sarpong - Jun 5 - - Dev Community

Intro

We will configure Git to use your work GitHub account and work SSH key for all repositories in your work directory, while using your personal account and SSH key for repositories in other directories.

With this guide you won't have to use weird Github urls or edit your ssh config.

Requirements

Make sure you have generated ssh keys for your work account and your personal account.

Let's call them id_rsa_work and id_rsa_personal respectively.

These instructions are for Mac and Linux. Windows users will have to adapt it.

Steps

1 Create a config file in your home directory called .gitconfig-work and put this in it.

[user]
    name = Your Name
    email = workemail@company.com
[core]
    sshCommand = "ssh -o IdentitiesOnly=yes -i ~/.ssh/id_rsa_work -F /dev/null"
Enter fullscreen mode Exit fullscreen mode
  • change Your Name to your name
  • change workemail@company.com to your work email
  • change ~/.ssh/id_rsa_work to the location of the ssh private key used for your work github account.

2 Open your git config at ~/.gitconfig and put this in it

[user]
    name = Your Name
    email = personalemail@gmail.com

[core]
    sshCommand = "ssh -o IdentitiesOnly=yes -i ~/.ssh/id_rsa_personal -F /dev/null"

[includeIf "gitdir:~/work/"]
  path = .gitconfig-work
Enter fullscreen mode Exit fullscreen mode
  • change Your Name to your name
  • change personalemail@gmail.com to your personal email
  • change ~/.ssh/id_rsa_personal to the location of the ssh private key used for your personal github account.
  • make sure that .gitconfig-work is the name of the file you created in the previous step.

Conclusion

We have set up Git to use your personal GitHub account and personal SSH key as the default for all repositories on your computer, except for those in your work directory, which should use your work account and work SSH key.

. . . . . .