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.
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:
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.
~/.ssh folder will look something like this
~/.ssh/config file needs to look like this. (Don’t worry, explanation follows)
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.
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 firstname.lastname@example.org:storetools/app-test.git
Similarly, for all git commands with a remote url you’ll add the suffix.
git remote add origin email@example.com:storetools/app-test.git git remote set-url origin firstname.lastname@example.org:storetools/app-test.git
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 "firstname.lastname@example.org"
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 to set the current user.
Here’s what you have to do:
- (One time step) Set up the keys and ~/.ssh/config file
- When you clone a project you add your suffix to your url host
- Change commit email by running
That’s all! You’re all set to commit and push as you normally would.