log: 2

How to Setup Multiple Github Accounts on a Single Machine

7/28/2023

gitSSHSourcetree

Let’s dive in.

I have two example github accounts:

  • https://github.com/shmkl-work
  • https://github.com/shmkl-personal

I want to setup my mac to manage Git from multiple accounts, respectively.

Note: The application of this logic is not limited to just two accounts; it can be extended to multiple accounts as well.


Steps:

  • Step 1 : Generate SSH keys
  • Step 2 : Add new SSH keys to the SSH Agent
  • Step 3 : Add SSH public key to Github
  • Step 4 : Create a Config File / Make Host Entries
  • Step 5 : Clone GitHub repositories using various accounts
  • Step 6 : Update git config for Repositories
  • Step 7 : Sourcetree SSH Key Detection (Optional)

Step 1:

Generate SSH Keys

Change directories so that your current directory is the .ssh folder.

     $ cd ~/.ssh

Generate an unique SSH key (replace with your e-mail/filename):

     ssh-keygen -t ed25519 -C "your_email@example.com" -f "github-username"
  • ssh-keygen: This is the command-line tool used for generating SSH keys.
  • -t ed25519: This option specifies the type of key to generate, which, in this case, Ed25519 is a modern elliptic curve algorithm widely used for secure communications.
  • -C: This option allows you to provide a comment that will be included in the generated public key. It is typically used to identify the purpose or owner of the key.
  • -f: This option specifies the filename for the generated key pair.

Note: If you are using a legacy system that doesn’t support the Ed25519 algorithm, use:

    ssh-keygen -t rsa -b 4096 -C "your_email@example.com"

Generate the SSH keys for two accounts

    ssh-keygen -t ed25519 -C "your_work_email@example.com" -f "github-shmkl-work"
    ssh-keygen -t ed25519 -C "your_personal_email@example.com" -f "github-shmkl-personal"

Notice shmkl-work and smkl-work are the username of my github accounts corresponding to your_work_email@gmail.com and your_personal_email@gmail.com email ids respectively.

After entering the command the terminal will ask for passphrase, which is optional, and would only need to be entered once.

After generating the SSH keys, in your .ssh folder, a public key and a private will be available.

The public key will have the extention .pub and private key will not. Both keys have identical names which were passed after the -f option in the above command. (In my case github-shmkl-work and github-shmkl-personal)


Step 2:

Add new SSH keys to the SSH Agent

Once we have the keys, they are not functional until we add them to the SSH Agent.

     ssh-add -apple-use-keychain ~/.ssh/github-shmkl-work
     ssh-add -apple-use-keychain ~/.ssh/github-shmkl-personal

Learn more about adding SSH keys to the SSH Agent here.


Step 3:

Add SSH public key to Github

Add your public key (generated from the previous step) to the corresponding github accounts.

1. Copy the public key

We can copy the public key either by opening the github-shmkl-work.pub file in vim and then copying the content of it.

     vim ~/.ssh/github-shmkl-work.pub
     vim ~/.ssh/github-shmkl-personal.pub

OR

We can directly copy the content of the public key file in the clipboard.

     pbcopy < ~/.ssh/github-shmkl-work.pub
     pbcopy < ~/.ssh/github-shmkl-personal.pub

2. Paste the public key on Github

  • Sign in to Github Account
  • Go to Settings > SSH and GPG keys > New SSH Key
  • Paste your public key and give it a title of your choice. For example, Computer, Model, Year.

OR


Step 4:

Create a Config File / Make Host Entries

The ~/.ssh/config file allows us specify various config options for SSH.

If config file doesn’t already exist, create one (ensure you are in the ~/.ssh directory)

     touch config

The command below opens the config file in your default editor, which is likely to be TextEdit or VS Code.

     open config

Next, we must add these lines to the file, with each block corresponding to the respective account we created earlier.

     Host github-shmkl-work
          HostName github.com
          User shmkl-work
          IdentityFile ~/.ssh/github-shmkl-work

     Host github-shmkl-personal
          HostName github.com
          User shmkl-personal
          IdentityFile ~/.ssh/github-shmkl-personal

Step 5:

Clone GitHub repositories using various accounts

With our setups completed, it’s time to see them in action. Let’s clone a repository using one of the accounts we added.

Create a new project folder in the location where you want to clone your repository, and navigate to that directory using your terminal.

For Example:

I am creating a repository on my personal GitHub account named TestRepo. To clone the repository, use the following command:

    git clone git@github-{your-username}:{owner-user-name}/{repo-name}.git

    [e.g.] git clone git@github.com-shmkl-personal:shmkl-personal/TestRepo.git

Step 6:

Update git configs for Repositories

From now on, to ensure that each repository on the system correctly associates commits and pushes with the respective GitHub user, we need to configure user.email and user.name in every freshly cloned or existing repository.

To accomplish this, use the following commands:

     git config user.email "your_work_email@example.com"
     git config user.name "shmkl-work"

     git config user.email "your_personal_email@example.com"
     git config user.name "shmkl-personal"

Choose the appropriate pair for your repository accordingly.

To push or pull from the correct account, we must add the remote origin to the project.

     git remote add origin git@github-shmkl-personal:shmkl-personal

     git remote add origin git@github-shmkl-office:shmkl-work

Common git commands are now available:

     git push

     git pull

Step 7:

Sourcetree SSH Key Detection (Optional)

If you’re interested in adding your SSH keys to Sourcetree, you’ll need to rename your SSH keys files accordingly so that Sourcetree detects them when you go to add the accounts in Settings > Accounts.

     mv ~/.ssh/{username}-personal ~/.ssh/{username}-personal-GitHub

     mv ~/.ssh/{username}-work ~/.ssh/{username}-work-GitHub