Published on

Double Github accounts setup

Authors

Sometimes, using two Github accounts can be very convenient. Mostly it is needed to split various kind of work, but what if there is a need to set up both accounts on one computer for a time being?

This post is a step-by-step guide on how to make such setup.

Overview

In general, new Github account setup process could be split into several steps:

  1. Generate new SSH key
  2. Add SSH key to the SSH-agent
  3. Add SSH key to Github account

For two-accounts setup, I recommend set up both accounts at the same time to make sure names are not overlapping.

Setup

  1. Generate keys for both accounts via terminal:
ssh-keygen -t ed25519 -C "your_email@example.com"
  • Use corresponding emails for each of the keys.
  • When prompted to "Enter a file in which to save the key," enter a unique name for this key file, for example, /Users/yourusername/.ssh/id_ed25519_github_work.
  1. To manage your SSH keys and remember your passphrase, add your SSH private keys to the ssh-agent.
eval "$(ssh-agent -s)"

Create or open your ~/.ssh/config file with a text editor:

 open -a TextEdit ~/.ssh/config

Add the following entries to the config file, adjusting paths and Hosts as necessary:

# Working account
Host github.com-work
  HostName github.com
  User git
  AddKeysToAgent yes
  UseKeychain yes
  IdentityFile ~/.ssh/id_ed25519_github_work

# Personal account
Host github.com-personal
  HostName github.com
  User git
  AddKeysToAgent yes
  UseKeychain yes
  IdentityFile ~/.ssh/id_ed25519_github_personal

Add Your SSH Keys to the SSH-Agent.For each key, run:

ssh-add --apple-use-keychain ~/.ssh/id_ed25519_github_work
ssh-add --apple-use-keychain ~/.ssh/id_ed25519_github_personal
  1. Copy the SSH Key to Your Clipboard:
pbcopy < ~/.ssh/id_ed25519_github_work.pub
  • Go to GitHub > Settings > SSH and GPG keys > New SSH key.
  • Paste your key, provide a title, and click "Add SSH key".
  • Repeat for the second account.

Usage

When working with repos (cloning, setting up remote, etc.), add account reference to the name of the repo.

When cloning a repository, use the modified Host from your SSH config file instead of github.com. For example:

git clone git@github.com-work:Username/RepositoryName.git

This setup allows you to use multiple GitHub accounts on the same MacBook without conflict, by managing which SSH key is used for each repository.

Also, consider setting up proper user name and email for different repositories either globally or locally.

Additional reading

In case of any complications, it's always the best to refer to official guidelines:

  1. Connecting to GitHub with SSH
  2. Generating a new SSH key and adding it to the ssh-agent
  3. Adding a new SSH key to your GitHub account

You may also want to check out:

Have fun! ⌨