Effectively Handling Multiple Git Accounts on a Single Machine

Antonio Robson de Paula
The Startup
Published in
4 min readAug 3, 2020

--

Photo by chuttersnap on Unsplash

If you also need to juggle between work accounts and your own personal Git you might get yourself dealing with customizing Git configuration for each individual repository.

In this article, I am going to share a directory structure suggestion that combined with conditional include will help you achieve a more efficient way that works on any Unix-like (Mac, Linux etc.) machine.

For the remainder of this article we are going to suppose you have two work accounts and a single personal account.

Keys, keys and more keys

Using SSH keys allows you to avoid typing username and password for Git operations greatly speeding things up.

In order to create one key for each of the accounts you are going to use the ssh-keygen command, make sure that when asked for a passphrase you leave it blank (just press Enter twice):

$ cd ~/.ssh
$ ssh-keygen -t rsa -C "remi.byers@protonmail.com" -f "personal"
$ ssh-keygen -t rsa -C "remi.b@work1.io" -f "work1"
$ ssh-keygen -t rsa -C "r.byers@work2.com" -f "work2"

The username is intended to be fictional.

Once finished the result will be three pairs of keys (public and private) for each account, like so:

$ ls -la ~/.ssh
total 64
drwx------ 10 remb staff 320 Jul 30 11:02 .
drwxr-xr-x+ 31 remb staff 992 Aug 5 16:01 ..
-rw------- 1 remb staff 434 Jul 23 08:51 config
-rw------- 1 remb staff 2602 Jul 22 13:43 personal
-rw-r--r-- 1 remb staff 567 Jul 22 13:43 personal.pub
-rw------- 1 remb staff 2610 Jul 23 08:43 work1
-rw-r--r-- 1 remb staff 574 Jul 23 08:43 work1.pub
-rw------- 1 remb staff 2610 Jul 22 13:44 work2
-rw-r--r-- 1 remb staff 574 Jul 22 13:44 work2.pub
-rw-r--r-- 1 remb staff 1718 Jul 31 15:20 known_hosts

The contents of each public key (the files ending with .pub) needs to be added on the corresponding Git account.

Copy the content of the personal account:

$ pbcopy < ~/.ssh/personal.pub

Having logged to Git using your personal account, navigate to the appropriate section: GitSettingsSSH and GPG Keys.

Once you reach this page, click on New SSH key and paste the contents of your personal account public key on the Key field and also choose a proper Title (usually your computer name or description).

Repeat these steps for each of the work accounts you have.

Once done it is a good idea to benefit from the lack of adding a passphrase to each of the keys and let them be loaded when necessary.

Open the SSH configuration file:

$ nano ~/.ssh/config

And add each key as a corresponding IdentityFile, like so:

# Personal account
Host github.com
HostName github.com
User git
IdentityFile ~/.ssh/personal
# Work1 account
Host github.com-work1
HostName github.com
User git
IdentityFile ~/.ssh/work1
# Work2 account
Host github.com-work2
HostName github.com
User git
IdentityFile ~/.ssh/work2

Great, now you no longer need to enter a username and password each time you issue a Git command.

On the next part of this article we are going to explore how to customize configurations for each of the accounts.

The directory structure

To accomodate both work accounts and the personal account, your directory structure would look like this:

.
└── ~/
└── Developer/
├── work1/
├── work2/
└── personal/

The idea here is that all repositories related to the work1 account are going to reside under ~/Developer/work1/. The same occurring for work2 repositories and your personal account repos.

You can always get creative and arrange in other ways so that the structure accommodates your workflow and number of accounts. The important thing is that you reflect it on the configuration files as shown in the next section.

Configure Git

Start by modifying the global Git configuration file, like so:

$ nano ~/.gitconfig

Add the conditional include sections so you end up with a content like the one below:

[user]
name = Remi Byers
email = remi.byers@protonmail.com
[includeIf "gitdir:~/Developer/work1/"]
path = ~/Developer/work1/.gitconfig
[includeIf "gitdir:~/Developer/work2/"]
path = ~/Developer/work2/.gitconfig

This global configuration is making the personal account default, this is the reason why you don’t see a .gitconfig override for the personal folder.

Now add the first work specific configuration file:

$ nano ~/Developer/work1/.gitconfig

And specify each property you want to be different from your global Git configuration. The example below only replaces the commit email address:

[user]
email = remi.b@work1.io

Do the same for your second work specific configuration file:

$ nano ~/Developer/work2/.gitconfig

Configure the properties that need to be different:

[user]
email = r.byers@work2.com

And you are done preparing your multiple Git account folder and configuration structure. In case you need to adjust other parameters you can check.

Be more seamless

With SSH keys, directory structure and configuration files you can switch seamlessly from work related and personal repositories without the need of making adjusts after each git clone avoiding falling in traps like pushing code with your work email to your personal repos 🙃.

--

--