GGK

4 Oct 2022 ~ 3 min read

Git with Multiple Email


If you work with multiple clients or projects and these clients require you to have a new email for their GitHub/GitLab access, It’s not obvious what to do with git. I understand the frustration, You want to use your personal email for your projects and don’t want to use a different computer/account for each client you have.

Table of Contents


Step 1: The SSH keys

Password authentication is now long gone. SSH is the better and safer option. GitHub and GitLab does not let you use one SSH key for different accounts, so we need to generate an ssh key pair for all clients and have one for your personal account.

This guide assumes you have already set up your SSH keys with GitHub or GitLab. If no, follow the respective guides:

  1. Guide by GitHub
  2. Guide by GitLab

While doing this, make sure you do not delete the existing values inside your ~/.ssh/config file. For every client you work with you’ll make a new host entry. There is something to fix here, which we’ll do in a later step.

Your ~/.ssh folder will look something like this

.ssh folder

Your ~/.ssh/config file needs to look like this. (Don’t worry, explanation follows)

.ssh config

You’ll notice something weird about the host parameter. Let’s take a single host entry as an example

# Your personal account
Host github.com
    HostName github.com
    User git
    UseKeychain yes
    AddKeysToAgent yes
    IdentityFile ~/.ssh/id_ed25519_github
	IdentitiesOnly yes

# Storetools
Host github.com-st
    HostName github.com
    User git
    UseKeychain yes
    AddKeysToAgent yes
    IdentityFile ~/.ssh/id_ed25519_storetools
	IdentitiesOnly yes

The reason you add a suffix to github.com (github.com to github.com-st) is to have a way to differentiate between different keys. This can be anything. You can replace github.com entirely with something else. I prefer a suffix. Let me show you why.

Clone

Now, when you clone a repo (or set a remote). You’ll use the ssh url and add the suffix to the host.

git clone git@github.com-st:storetools/app-test.git

Git remote

Similarly, for all git commands with a remote url you’ll add the suffix.

git remote add origin git@github.com-st:storetools/app-test.git
git remote set-url origin git@github.com-st:storetools/app-test.git

Step 2: Commit Email

You have now cloned or created a repository successfully. Now for every commit you still use a default email which is set in the global git config.

I have 2 ways for you to do this. I prefer the second.

1. Default git way

You can use these commands to set the global and per repository(local) commit name and email using git

# Global
git config --global user.name "Geon George"
git config --global user.email "email@example.com"

# Local
git config user.name "Geon George"
git config user.email "email@example.com"

2. Git User Switch

Install this NPM package globally: Git User Switch. (Documentation in the GitHub README).

npm i -g git-user-switch

With this, all you have to do is run git-user or git user to set the current user.

Git-user-switch

Sum Up

Here’s what you have to do:

  1. (One time step) Set up the keys and ~/.ssh/config file
  2. When you clone a project you add your suffix to your url host
  3. Change commit email by running git-user

That’s all! You’re all set to commit and push as you normally would.


Headshot of Geon George

Hi, I'm Geon. I'm a software engineer based in Kerala, India. You can follow me on Twitter, see some of my work on GitHub.