Use multiple GIT accounts on same machine / macOS

The problem_

In my personal case I use three different git services for store / share my personal / work code:

  1. Github // my blog code, free open sources code
  2. Gitlab // my work code, on-premise installation
  3. Bitbucket // my personal code, free private repo

Every service uses different SSH keys to use. Must be configured separately.


Creating different SSH keys_

Open your favorite Terminal (I use a macOS, Catalina 10.15 in this post) and start creating your different keys:

Use the command: $ ssh-keygen -t rsa -b 4096 -C “email@domain.com” to create a key for each git service you want to use.

The email@domain.com is the associated email of your service. You can login into github/gitlab/bitbucket and retrieve it.

After this command, ssh-keygen asks you to choose a name, if not it uses the default one, id_rsa.

To separate each ssh keys, I suggest using the same prefix adding the service name you want, like id_rsa_gitlab, id_rsa_github and id_rsa_bitbucket and so on…

[~/.ssh]$
[~/.ssh]$ ssh-keygen -t rsa -b 4096 -C "email@domain.com"
Generating public/private rsa key pair.
Enter file in which to save the key (/Users/name/.ssh/id_rsa): id_rsa_gitlab
[...]
[~/.ssh]$

After this, list your ~/.ssh folder and you should see a pair of file for each service, like this (one is the private key, the .pub is the public one):

id_rsa_gitlab
id_rsa_gitlab.pub
id_rsa_github
id_rsa_github.pub
id_rsa_bitbucket
id_rsa_bitbucket.pub

Add SSH key to your account

Every GIT service has a key setting configuration page:

Github: https://github.com/settings/keys

Gitlab: https://gitlab.com/profile/keys

BitBucket: https://bitbucket.org/account/user/[…]/ssh-keys/

Copy the content of your relative .pub file and paste to your git online services, in the relative configuration page:

$ pbcopy < ~/.ssh/id_rsa_gitlab.pub

Create a local configuration file

The last step is to create a configuration file that redirects automatically the ssh-keys to the right service.

This file must be created in the ~/.ssh folder.

$ vi config

Edit this file adding your preferences, for instance:

# GITHUB account
Host github.com-opensources
   HostName github.com
   #HostName enterprise.github.com
   User git
   IdentityFile ~/.ssh/id_rsa_github

# GITLAB account
Host gitlab.com-work
   HostName gitlab.com
   #HostName company.gitlab.com
   User git
   IdentityFile ~/.ssh/id_rsa_gitlab

# BITBUCKET account
Host bitbucket.org-personal
   HostName bitbucket.org
   User git
   IdentityFile ~/.ssh/id_rsa_bitbucket

Every GIT service is now configured and linked to your SSH-key.

Remember to set up your GIT global settings (email and name):

git config --global user.name "Name Surname"
git config --global user.email "username@email.com"

Sourcetree

The Terminal, of course, is the best solution to manage git commits / push / branches and so on.. but:

  • if you have a conflict?
  • If you want to merge and align different branches in one shot?
  • If you want to rollback to a specific commit id, reading your git commit messages?
  • Want to discard a single line in a particular file?
  • Want to see what you edit and what to discard?

For every “complicated” task using git, (for complicated I mean something that you don’t remember how to do it when happens), I use Sourcetree, a macOS utility from Atlassian (Bitbucket) that helps you via graphical interface to use all “complicated” commands in an easily way, without making damages (you see what you’re doing before doing!).

You can download here: https://www.sourcetreeapp.com/ 

PS: Sourcetree doesn’t pay me for this 😉 but I like a lot and I want to share!

Alberto Pasca