Using multiple Git accounts and different Git platforms can be a challenge. In this post I explain how to configure multiple accounts, use different SSH keys and even verify your account.

Introduction

Working with different Git accounts can be a challenge. Speaking for myself: I normally work with at least 3 accounts, currently even four: a personal GitHub account, a personal Microsoft DevOps account and both a DevOps and a GitLab account for work.

In this post I want to explain how I did my setup, hopefully it might be a help to you too. By the way: I use Linux, so things might be a bit different for you if you use either a Mac or Windows.

Different Git Settings

Basic settings

One of the first things I do when starting with a new system is setting up Git (the main reason being to get my dotfiles). I will set my username and email address:

git config --global user.name "Jacob Duijzer"
git config --global user.email "EMAIL HERE"

This will create a config file in my home directory which looks like this:

[user]
    name = Jacob Duijzer
    email = EMAIL HERE

This means that when I commit anything to a Git repository, this is the name and email address which will be used.

Adding another user

I might be using a different user for different Git accounts. Maybe for work, a different project or any other reason.

There might be other ways to configure this, but this is what I usually do: I define a directory in which I have one or multiple Git repositories. Let’s use the work example. I store all work related projects in the directory `~/projects/work/`. To use another git account for this directory, I add the following to my `~/.gitconfig` file:

[user]
    name = Jacob Duijzer
    email = EMAIL HERE

[includeIf "gitdir:~/projects/work/"]
    path = .gitconfig_work

I will add a different username and email address to the `~/.gitconfig_work`:

[user]
    name = Jacob Duijzer [Employer Name]
    email = WORK EMAIL HERE

When I commit work inside any of the directories inside my work directory Git will use the user and email configured in the `~/.gitconfig_work` file. Make sure you add the `/` at the end of the path, otherwise it will only work for the directory itself, not for all directories inside that directory.

Different SSH keys

If you are not using SSH to clone repositories yet, now might be a good time to start doing this! It is a more secure way and, in fact, once configured very easy to use (although a bit more difficult when using multiple accounts).

The process of generating keys and setting them up in GitHub (or any other platform) is well documented, you can have a look here to set it up when it is your first time.

I repeated the process of generating SSH keys multiple times (remember to use different, meaningful names for the output files!). After creating multiple keys, I added them to my SSH configuration (`~/.ssh/config):

Host personal
  HostName github.com
  IdentityFile ~/.ssh/id_rsa
  User git

Host workname.github.com
  HostName github.com
  IdentityFile ~/.ssh/id_rsa_workname
  User git

Host devopsworkname
  HostName ssh.dev.azure.com
  IdentityFile ~/.ssh/id_rsa_workname_devops
  User jacob.duijzer@worknamead.onmicrosoft.com
Code Snippet 1: ~/.ssh/config

Checking out new repositories

Now, if you want to clone a repository with the user configured to the second key in the config above, it will fail. This can be solved by changing the clone URL of the repository.

Instead of this:

git@github.com:workname/test-repo.git

Execute this:

git@workname.github.com:workname/test-repo.git

So, I just replaced the hostname github.com with workname.github.com, and now it works!

Changing an existing repository

If you already checked out a repository, but you want to change the URL of this repository, simply change it in the git configuration of the cloned repository:

[core]
	repositoryformatversion = 0
	filemode = true
	bare = false
	logallrefupdates = true
	ignorecase = true
	precomposeunicode = true
[remote "origin"]
	url = git@workname.github.com:workname/test-repo.git
	fetch = +refs/heads/*:refs/remotes/origin/*
[branch "master"]
	remote = origin
	merge = refs/heads/master
Code Snippet 2: .git/config

I have changed the URL of the repository in above config, under the tag “remote” to `workname.github.com` instead of `github.com` and that is all!

Signing

If you want to be sure your commits are done by you, you can sign and verify your work. A verified commit will look like this:

Figure 1: Verified account in GitHub

Figure 1: Verified account in GitHub

To set up signing for multiple accounts, simply add the GPG Key id to the configuration file:

[user]
    name = Jacob Duijzer (Employer Name)
    email = WORK EMAIL HERE
    signingkey = XXXXXXXXXXX

[commit]
    gpgsign = true
Code Snippet 3: ~/.gitconfig_work

Just add the correct GPG id to every file and you have multiple verified accounts!