avatarKevin Kim

Summary

The web content provides a step-by-step guide on how to configure separate GitHub accounts for work and personal use on the same machine, specifically focusing on using SSH keys for authentication.

Abstract

The article outlines a process for managing commits to separate GitHub repositories using different SSH keys associated with distinct GitHub accounts. It begins with generating a new SSH key for the personal GitHub account, followed by adding this key to the personal GitHub account settings. The user then configures SSH to recognize the new key and sets up a config file to differentiate between the work and personal GitHub accounts. The guide also includes instructions for initializing a local Git repository, configuring the correct user email, and adjusting remote repository URLs to ensure commits are attributed to the appropriate GitHub account. The conclusion emphasizes that by following these steps, one can seamlessly push commits to the correct repository with the correct account credentials.

Opinions

  • The author has successfully combined a tutorial with insights from Stack Overflow to create a solution that fits their needs.
  • The author suggests that the provided method is effective for pushing commits to personal and work repositories without the need for repeated configuration changes.
  • The author implies that managing multiple GitHub accounts on one machine can be challenging but is made simpler with the use of SSH keys and a well-configured SSH config file.
  • The author indicates a preference for using SSH keys over other authentication methods for their convenience and security when dealing with multiple GitHub accounts.

How to push to a work repo and personal repo with separate GitHub accounts

Sometimes I just want to work on some side projects on my work laptop for personal learning and still light up my personal GitHub account squares with my personal Github account.

I used a mix of a tutorial and a Stack Overflow answers and this is what worked for me.

Step 1 — Create a New SSH Key

cd ~/.ssh
ssh-keygen -t rsa -C "[email protected]"
Generating public/private rsa key pair
Enter file in which to save the key (/Users/administrator/.ssh/id_rsa): 
/Users/administrator/.ssh/id_rsa_personal
Enter passphrase (empty for no passphrase)
Enter same passphrase again:
Your id has been saved in .....

Step 2— Enter SSH Keys into Personal Github Account

Log into personal github account, go to account settings, go to SSH Public Keys, set the title to whatever, and paste in Key from whatever is stored in your .ssh directory

vim id_rsa_personal.pub

copy the whole string from there, then paste it into the SSH Public Key section, click Add Key

Step 3 — Let SSH know about your newly created rsa

ssh-add ~/.ssh/id_rsa_personal
Identity added: ....

The bulk of the work has been done…

Step 4 — Create or edit a config file

cd ~/.ssh
# touch or edit config file
vim config

For me, this popped up

Host *
  AddKeysToAgent yes
  UseKeychain yes
  IdentityFile ~/.ssh/id_rsa

Then I changed it to:

Host github.com
  HostName github.com
  User git
  IdentityFile ~/.ssh/id_rsa
Host github-personal
  HostName github.com
  User git
  IdentityFile ~/.ssh/id_rsa_personal

Step 5: Create a local directory on your machine

cd into the local directory you created then do:

git init

Here, type to see what the user email is set to

git config user.email 

Most likely, it should be set to the default GitHub account, in my case, the work email.

Now do this:

git config user.email "my-personal-github-email@gmail.com"

Step 6: Create a repo on personal Github account

After that, there’s some tweaking you have to do…

Step 7: Remote adding tweaks

Usually, your git remote add convention will be just copypasta this:

git remote add origin [email protected]:kevinYCKim33/effective-journey.git

But…now it will be something like this

git remote add origin git@github-personal:kevinYCKim33/effective-journey.git

Now when I do:

git push -u origin master

I will now be able to push to my personal repo under my personal email.

I am still able to push to my work repo with my work account without needing any git remote tweaks.

Conclusion

TLDR: If I need to create a personal project on my work mac, all I have to do is follow Step 5~7 at this point. Then push to everything as needed, and all of that right repo with right person thing should be taken care of.

Sources

Quick Tip: How to Work with GitHub and Multiple Accounts: 90% here but it defaulted to letting me push to my personal repo with my work account.

Greg Leszek answer but with quotes around “[email protected]

Git
Github
Recommended from ReadMedium