Multiple users

Quite often we need to have multiple git users on the same workstation. For example:

  • a user for working account;
  • a user for personal account;

These users should have different name and email, and might need different configuration and authentication. Accounts for the users can be on different platforms (GitHub, GitLab, BitBucket etc) or on the same.

A simple solution would be to use repository-local (git config --local) configuration, but this means repeating it over and over again for every cloned repository. This is not an engineers way.

Configure git per directory

Git stores global configuration in ~/.gitconfig file. It’s a text file which supports conditional includes via includeIf directive. We’re interested in gitdir condition, which includes the file if the current repository (.git folder of the current repository) is in the specified directory.

So, we can nicely isolate all work-related repositories in ~/work directory and all personal repositories in ~/personal directory. For example, the main ~/.gitconfig file may look like:

[user]
    name = Alex Green
    email = alex.green@company.com

[includeIf "gitdir:~/work/"]
    path = ~/.gitconfig-work

[includeIf "gitdir:~/personal/"]
    path = ~/.gitconfig-personal

while ~/.gitconfig-work enables commit signing for work account:

[user]
    signingkey = XXXXXXX

[gpg]
    program = gpg

[commit]
    gpgsign = true

and ~/.gitconfig-personal redefines user name and email for personal account:

[user]
    name = Mr Green
    email = mr-green@personal.com

See:

~/work/company-project $ git config --get user.name
Alex Green

~/personal/awesome-project $ git config --get user.name
Mr Green

ssh authentication

Most git platforms (GitHub, GitLab, BitBucket etc) support ssh authentication, and we can configure it in ~/.ssh/config file of our ssh client:

Host github.com
  IdentityFile ~/.ssh/github

Host bitbucket.com
  IdentityFile ~/.ssh/bitbucket

Then both

  • git clone git@github.com:organization/repository.git
  • git clone git@bitbucket.org:organization/repository.git

work and use corresponding ssh keys.

ssh authnetication for the same host

But what if we have multiple accounts on the same platform (both accounts are on GitHub for example)? Well, ssh client doesn’t know which key to use, so we need to tell it via different host. We can configure the ssh client with artificial host for one of the accounts (for example for personal account):

Host github.com-personal
  IdentityFile ~/.ssh/github-personal

Host github.com
  IdentityFile ~/.ssh/github-work

Then git clone git@github.com:organization/repository.git will use ~/.ssh/github-work key.

But in order to clone a repository using personal ~/.ssh/github-personal key, we need to change the host in url manually (note github.com-personal instead of github.com):

git clone git@github.com-personal:organization/repository.git

This means we can’t use the “default” clone command provided by the platform, we need to manually adjust it.

Hint: it’s generally more convenient to have the “default” configuration for the account which is used to clone repositories more often, because we can use the “default” clone command from the platform.

Some references:

Commit signing

Some platforms may require commit signing. I use gpg key for that. GitHub documentation explains how to set it up. We can also configure git to sign our commits by default:

[user]
    signingkey = XXXXXXX

[gpg]
    program = gpg

[commit]
    gpgsign = true