Balancing Personal and Professional: Manage Two GitHub Accounts

July 21, 2023 (2y ago)

74 views

We all know it's essential to separate our work and personal lives. We try to avoid bringing office work home, and similarly, we don't want our personal hobbies interfering with our professional responsibilities. Just like we have separate emails for work and personal use, it's beneficial to have separate GitHub accounts for your professional and personal projects.

In this blog post, we're going to walk through the process of managing a personal and a work GitHub account on the same machine.

Step 1: Crafting the Keys to Your Accounts

Let's begin by creating unique SSH keys for each of your GitHub accounts. Consider these SSH keys as individual access passes to your personal and professional GitHub realms. Here's how you can create an SSH key:

$ ssh-keygen -t rsa -b 4096 -C "personal-email@example.com"

Upon being asked where to save the key, specify a unique location and name like so:

Enter file in which to save the key (/Users/you/.ssh/id_rsa): /Users/you/.ssh/id_rsa_personal

Repeat this process for your work account, ensuring you give it a unique name like id_rsa_work.

Step 2: Assigning Keys to Their Respective Accounts

The next step is similar to registering your access pass (SSH key) with the respective realms (GitHub accounts). Here's how you can do this:

  • Start by copying your SSH key, as if you're taking a snapshot of it:

    $ pbcopy < ~/.ssh/id_rsa_personal.pub
  • Now, sign in to your personal GitHub account. Go to Settings, find SSH and GPG keys, then click New SSH key.

  • Here, provide a label for your key, something like "Personal MacBook Air", then paste your key into the "Key" field, and click Add SSH key.

  • Repeat this process for your work GitHub account using the id_rsa_work key.

Step 3: Educating Your Machine

Now that you've created and assigned keys, your computer (more specifically, SSH) needs to understand which key corresponds to which GitHub account. To achieve this, you'll create a config file:

$ cd ~/.ssh/
$ touch config
$ open -e config

In this file, add an entry for each key:

# Personal GitHub account
Host github.com-personal
  HostName github.com
  User git
  IdentityFile ~/.ssh/id_rsa_personal
 
# Work GitHub account
Host github.com-work
  HostName github.com
  User git
  IdentityFile ~/.ssh/id_rsa_work

This information enables SSH to differentiate between the keys and the associated GitHub accounts.

Step 4: Ensuring Correct Account Usage

The final step is to ensure that when you're working on a project, Git uses the right GitHub account. This is similar to making sure you're not discussing work matters during your personal time and vice versa. In your project directory, execute the following:

$ git config user.name "Your Name"
$ git config user.email "personal-email@example.com"

Also, change the remote URL to the appropriate SSH URL:

$ git remote set-url origin git@github.com-personal:username/repo.git

Be sure to replace "username" and "repo" with your GitHub username and repository name, respectively.

And that's it! You now have separate keys to your personal and professional GitHub realms, and you're equipped to use them correctly. This setup enables you to smoothly switch between your GitHub accounts, maintaining the necessary boundaries between your personal and professional coding lives. Happy coding!