Managing Multiple GitHub Accounts on a MacOS: A Step-by-Step Guide

Viral Thaker
6 min readJan 28, 2024

--

Introduction:

Alright, let’s talk GitHub juggling — because who doesn’t love managing not one, but two active GitHub accounts? I found myself in the same boat, straddling my personal account and my work account, and boy, did it turn into a rollercoaster ride on my system 😪.

Imagine this: you’re in full coding mode, rocking those commits like a true coding maestro 🚀. Now, weekends roll in, and you want to dive into your personal projects or maybe sprinkle some contributions on open source using your personal account. But, ah, the struggle! Switching machines or doing the whole log-out-log-in dance from work to personal account? Total hassle..That’s when the need for a smooth transition between GitHub identities became my coding mission.

In this article, I’m not here to give you the “official” method — I’m here to share my battle scars and the nifty hacks I discovered along the way.

While this article will not be 100% accurate to other OS platforms the core idea will be the same more or less.

1. Creating SSH keys for all accounts 🔑:

So, you’ve got multiple GitHub accounts, and you don’t want them to get into a key-related fight, right? Well, here’s how you create personalized keys for each of them:

(p.s: In my situation, I had two accounts but you can add as many accounts as you want)

1.1 Where to Start: Find your .ssh folder:

  • Make sure you’re in the right neighborhood. Run this command to get there:
$ cd ~/.ssh

1.2 Let’s Make Some Keys: No Abracadabra Needed 🧙‍♂️:

Time to create your keys. Each account gets its own special key:

$ ssh-keygen -t rsa -C "my@personal" -f "github-personal"
$ ssh-keygen -t rsa -C "my@work" -f "github-work"
  • -t input: The type of key to create.
  • -C "input": A little note to remember which key is which.
  • -f "input": The name of the key files.
  • You can choose how to name the key pair. I used github-{context}.
  • you maybe prompted to enter passphrase if you want to protect your public key access with extra layer of protection. You can skip it by leaving it blank.

1.3 Key Duo: Public and Private 🔐:

  • Now, you’ve got a pair of keys for each account — one public (github-personal.pub) and one private (github-personal). Same for the work public & private keys.

2 Adding Keys to Your Digital Wallet: The SSH Agent.

But hold on, these keys are just born 🤔, they need to be introduced to your SSH agent. Think of ssh-agent as your trusty backstage manager. It securely handles and stores the keys for accessing your GitHub accounts, eliminating the passphrase hassle.

2.1 Add keys to SSH agent:

$ ssh-add --apple-use-keychain ~/.ssh/github-personal
$ ssh-add --apple-use-keychain ~/.ssh/github-work
  • The --apple-use-keychain option is a command-line parameter used with the ssh-add command on macOS. When you run ssh-add --apple-use-keychain, it instructs the SSH agent to store the private keys in the system's Keychain, a secure password management system on macOS. You can learn more about it here.

2.2 Adding Public Keys to GitHub:

  • Visit here while logged in to the corresponding GitHub account.
  • Click the ‘New SSH key’ button and paste the public key from clipboard.
$ pbcopy < ~/.ssh/github-personal.pub
$ pbcopy < ~/.ssh/github-work.pub

3. Creating GitHub Host Entries in SSH Config:

Alright, we’ve got our SSH keys lined up, but now we need to tell our machine which key goes where. Time to create custom host entries in the SSH Config file, your GitHub navigation system.

This is one the most Important step, lets do it slowly and nicely :

3.1 Crafting Host Aliases: Your GitHub Secret Handshake:

  • open the SSH Config file:
$ open -e ~/.ssh/config
  • Now, it’s like setting up personal shortcuts for each GitHub account. Add these lines to the file:
# Your Personal GitHub Account
Host github.com-personal
HostName github.com
User git
IdentityFile ~/.ssh/github-personal

# Your Work GitHub Account
Host github.com-work
HostName github.com
User git
IdentityFile ~/.ssh/github-work
  • Host: The alias for the GitHub account.
  • HostName: The actual GitHub domain.
  • User: Always set to git.
  • IdentityFile: The path to the specific SSH key for that account.

Remember the Host value very carefully, we will need it in future.

3.2 Default Account Magic:

  • Now, let’s make life even simpler. Specify a default GitHub host globally:
$ git config --global user.name "your_personal_username"
$ git config --global user.email "personal@example.com"
  • Replace the user.name with your personal username and user.email with personal account attached email if you want to set your default account to personal. If you want to make your work account default replace work associated values.
  • This way, when you’re casually coding without mentioning the host explicitly, Git knows which account to associate your commits with.

4. Using multiple accounts simultaneously :

That’s it, we have successfully added multiple accounts side by side. However, Let’s understand how to use it simultaneously without logging in and logging out back and forth.

4.1 Fresh Clone Protocol 🫡:

For repositories that you haven’t cloned yet, here’s the protocol:

  1. Go to the root folder where you want the cloned repository to reside.
  2. Use the following format for the clone command:
$ git clone git@github.com-youraccount:owner/repo.git
  • Replace youraccount with the corresponding alias you defined in your SSH Config file, and owner/repo with the actual repository path.
  • For example:
$ git clone git@github.com-personal:yourusername/cool-project.git
  • In this example, I wanted to access and work against my personal repository which required me to have my personal account instead of work.

6.2 Updating Remotes for Existing Repositories: Switching Gears Mid-Journey:

For repositories you’ve already cloned, changing the user involves three steps:

  1. Navigate to the repository’s root folder:
  2. Execute the following commands to update the remote URL:
$ git remote rm origin
$ git remote add origin git@github.com-youraccount:owner/repo.git
$ git remote set-url origin git@github.com-youraccount:owner/repo.git
  • replace youraccount, owner, and repo with the appropriate values.
  • For example:
$ git remote set-url origin git@github.com-personal:yourusername/cool-project.git

3. Finally, ensure that your local Git configuration reflects the correct user information:

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

That’s it! You’re now set to seamlessly navigate between your GitHub multiverse, cloning and updating repositories with the right identities.

5. You might need to add the SSH key again:

Sometimes, your system is restarted/shut down. In that case, one of your SSH accounts might have been removed (it can be another reason as well), so if you ever face an error again while accessing a different GitHub account make sure to check whether SSH keys are still set up correctly or not.

  • To check type in ssh-add -l in the terminal and verify all the keys
  • if the key for a specific profile is not found add it by following step 2 again.
  • this time however you only need to follow step 2.1 and skip the rest as other steps are one time only.

Conclusion: Navigating GitHub, Your Way:

And there you have it — a personalized GitHub setup tailored to your coding lifestyle. We’ve equipped our coding arsenal with distinct SSH keys, navigated GitHub with custom host entries, and ensured a seamless coding flow with Git defaults.

Whether you’re effortlessly cloning repositories or effortlessly committing changes, this setup empowers you to switch between GitHub identities with ease. Your GitHub multiverse is at your fingertips, ready for all your coding adventures.

Happy coding!

--

--