heroPhoto by Drew Beamer on Unsplash

How to use different git emails for personal and work repositories on the same machine

I recently started working on some side hustle projects. To keep things organised, I created a new git account with a different email. When I tried to push to the git repository within the new git account, I got the following error.

Enumerating objects: 26, done.
Counting objects: 100% (26/26), done.
Delta compression using up to 12 threads
Compressing objects: 100% (16/16), done.
Writing objects: 100% (21/21), 69.90 KiB | 4.37 MiB/s, done.
Total 21 (delta 2), reused 0 (delta 0)
remote: GitLab: Author 'personal@mail.com' is not a member of team
To gitlab.work.co.uk:project/repo.git
 ! [remote rejected] feature/a -> feature/a (pre-receive hook declined)
error: failed to push some refs to 'git@gitlab.work.co.uk:project/repo.git'

(I removed some sensitive data.)

I didn't want to change my default git email, because I still want to use it for other git repositories. After some research, I've found two ways to solve my problem.

Method 1: use different git email based on repository path

This method involves adding some conditions to global git config. Then within each git repo, it will automatically figure out which email to use based on the path.

By default, the global git config looked something like this for me.

# ~/.gitconfig

    email = personal@email.com
    name = Hao Dong

1. Rename ".gitconfig" to ".gitconfig-personal"

Rename the global config to ~/.gitconfig-personal using the following command. It will now be our new config for any personal projects.

mv .gitconfig .gitconfig-personal

Don't worry, we will add the global config back later.

2. Create ".gitconfig-work"

Create the new config file for work.

touch ~/.gitconfig-work

Then add the following into the config file.

# ~/.gitconfig-work

    email = work@email.com
    name = Hao Dong

3. Create a new ".gitconfig"

Add a new global .gitconfig file, this will point to the other two custom config files.

touch ~/.gitconfig

Then add the following into the global config file.

# ~/.gitconfig

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

Source: https://git-scm.com/docs/git-config#_includes

Method 2: setting git email per repository

Personally, I would suggest going with Method 1, no matter your situation. But if you are confident it is a one-off case, then this method might be appropriate.

Navigate to the work repository, then at the root folder run the following command to change the email.

$ git config --local user.email name@work.com

Note: this command only affects the current repository. Any other repositories will still use the default email specified in ~/.gitconfig.

Question: I did all that, but the commit still says the wrong email

If you already committed the code change like I have, then you will have to go through each commit made prior to the email change and then update the email in the commit using the following command.

git commit --amend --author="Full Name <email@address.com>" --no-edit 
Buy Me A Coffee