DEV Community

Franz Wong
Franz Wong

Posted on

Set different email address for multiple git repositories

Problem

I use my own laptop to code for both personal and work projects. However, I want to use my work email address only for my work projects. But I am lazy to set it for each repository.

Here is my folder structure. All work projects are under the same folder ~/src/work. Other projects are personal projects.

~/src/work/project-i        # work project
~/src/work/project-j        # work project
~/src/playground/project-a  # personal project
~/src/bots/project-b        # personal project
~/src/devops/project-c      # personal project
Enter fullscreen mode Exit fullscreen mode

Solution

Step 1. Add an includeIf section to global git config file ~/.gitconfig. (Create this file if it does not exist)

[user]
    name = Franz
    # personal email address
    email = franz@whatever.com

[includeIf "gitdir:~/src/work/"]
    path = ~/src/work/.gitconfig
Enter fullscreen mode Exit fullscreen mode

Step 2. Add the work email address to our work git config file ~/src/work/.gitconfig. (Create this file if it does not exist)

[user]
    # work email address
    email = franz@serious.com
Enter fullscreen mode Exit fullscreen mode

After you have done these steps, when you commit code to your work projects, the email used in commit will be your work email address.

Top comments (0)