How to have multiple Git accounts on the same computer (MacOS)

Thiago Centurion
5 min readAug 25, 2021

--

Are you working on a job project and would like to use the same computer to develop a personal project? Have you ever made commits to the job project with your personal git account or commits to the personal project with your job git account?

I was trying to keep developing to my job project (that is in GitLab Enterprise), making commits with my job git account, and have my Github personal account registered on my machine to do some personal projects sometimes. So I noticed that I can do that using SSH for both.

Step 1. SSH key for the job account and the personal account

I’m presuming that you already at least have a registered ssh key in your ~/.ssh directory for your work. Usually in an enterprise project, it has a specific constraint for creating a ssh key. Sometime it is a rsa 4096 or a ed25519 SSH keygen. So find out this information to create your job SSH key.

Otherwise, if both of projects doesn’t have this specific approach, you can follow just like below:

Generate the SSH key for the first account

$ ssh-keygen -t rsa -C "first.email@gmail.com"

When you write this command, a new SSH key will be generated and you are going to see something like that:

Generating public/private rsa key pair.Enter file in which to save the key (/Users/{YOUR_USER}/.ssh/id_rsa):

If you already have an SSH key for your job account, so probably you already have a default filename just like id_rsa ou id_ed25519 . Here, don't worry about the name… usually people just use the default filename for job projects. But if you prefer to handle better your SSH keys, you can add a filename like id_rsa_<project_name_here> . For example, in this case, you just can write as below and press enter:

/Users/{YOUR_USER}/.ssh/id_rsa_medium

This is a real example, after writing it and pressing enter:

Generating public/private rsa key pair.Enter file in which to save the key (/Users/thiagocenturion/.ssh/id_rsa): /Users/thiagocenturion/.ssh/id_rsa_medium

Then you’ll see a message like that:

Enter passphrase (empty for no passphrase):

Unless you want to add a custom passphrase, you can just press enter twice and your SSH will be correctly created in the directory that you just defined above. In my case, /Users/thiagocenturion/.ssh/id_rsa_medium.

The key fingerprint is:
SHA256:qZw7BWqLWpAlbn1qhkuAPPBaKOqzAbf3vONZi2kLN/s first.email@gmail.com

Store that key in where you can look at it again later. You have to register it in your job git account online.

Generate the SSH key for the second account

$ ssh-keygen -t rsa -C "second.email@gmail.com"

Now, you have to set up a different filename. This is important to avoid overriding the other key you just created. In my case, i just added the name id_rsa_personal :

/Users/{YOUR_USER}/.ssh/id_rsa_personal

Copy the public key to the clipboard:

$ pbcopy < ~/.ssh/id_rsa_personal.pub

Step 2. Save the SSH Key to the Git host

Here, I'm using Github.com, but it is probably almost the same steps for others git hosts.

  1. Go to Settings.

2. From the left side menu bar, click on SSH and GPG keys and click on New SSH key.

3. Name it with a title. A good ideia is to add the computer name you're using, so in my case I titled it with that refers to my work's Macbook.

4. Paste the ssh public key you just copied with pbcopy < ~/.ssh/... and press Add SSH key.

Step. 3 Add an SSH agent

In your Terminal (I like iTerm with zsh settings, it's a personal suggestion), write ssh-add for each of the keys you created:

$ ssh-add ~/.ssh/id_rsa_medium
$ ssh-add ~/.ssh/id_rsa_personal

Step. 4 Config file for SSH keys

By default, a config file doesn't exists. So we'll create it using touch ~/.ssh/config. Then, open it using any text editor (like Visual Studio Code) or vim editor. I created a shortcut to open any file in Visual Studio Code with an alias vscode.

$ vscode ~/.ssh/config

Add configuration for each git account you created SSH keys. By the first, you can do it for the work config adding this:

# Account 1 (work) - Medium 
Host github.medium.com
Hostname github.medium.com
User git
IdentityFile ~/.ssh/id_rsa_medium
  • You can add a comment using # to create a title for each configuration. I wrote Account 1 (work) — Medium just for an example, but you can write anything you want.
  • In Host and Hostname it's important to write the exact name which is defined in git repository when you click to clone it using SSH way. For example, if you are under an enterprise project for GitHub, probably this would be something like github.medium.com:
git clone git@github.medium.com:first.email/repository_name.git

Then, add configuration for the personal account:

# Account 2 (personal) 
Host github.com
Hostname github.com
User git
IdentityFile ~/.ssh/id_rsa_personal

And that you can save it and close the file.

Step 5. Remote repository using SSH config

Now you're one step closer to happiness: Everything you have to do is configuring the repository using SSH configuration.

Go to the repository page and click on Code > Clone > SSH.

If you are going to create a new repository, use git remote add origin. Otherwise, if you already have a remote repository all you should do is to use git clone.

$ git clone git@github.com:second.email/repo_name.git

Now you have to associate your commits with the correct username (this is what we want first of all). To do that, go to the local repository for the job project and write your username and user e-mail using git config:

$ git config user.name "first.email"
$ git config user.email "first.email@gmail.com"

Once it is correctly set up, go to the personal local repository and write this:

$ git config user.name "second.email"
$ git config user.email "second.email@gmail.com"

That's all, we have it done! Even though it wasn't too detailed, I hope I could help you with this article.

--

--

Thiago Centurion

Senior iOS Engineer @ SwissBorg | SwiftUI, iOS Architecture & UX Design Specialist