Git config user.email for repositories in subdirectory
Ariel Bogdziewicz

Ariel Bogdziewicz @absoftware

About: iOS apps and PHP/MySQL backend services. Familiar also with C/C++, C#, web development and Linux systems.

Location:
Kristiansand, Norway
Joined:
Sep 3, 2020

Git config user.email for repositories in subdirectory

Publish Date: Sep 26 '20
10 3

Quick hint for developers working on source code from different Git accounts having different e-mails for them. Let say we develop our own projects and we support development for two other companies. Our folder structure could look like

~/.gitconfig
~/PersonalProjects/
~/CompanyAbc/.gitconfig
~/CompanyMno/.gitconfig

Of course we have configured default config as follows

user@mymac ~/ $ git config user.name "Firstname Lastname"
user@mymac ~/ $ git config user.email "user@myprivatebox.com"
user@mymac ~/ $ git config push.default simple
user@mymac ~/ $ git config core.editor emacs

We want different e-mails by default for all repositories in these directories without configuring this manually with command

user@mymac ~/CompanyAbc/website-repo (master) $ git config user.email "user@company-abc.com"

every time when wy clone new repository.

Solution

Main ~/.gitconfig file should look like

[user]
        name = Firstname Lastname
        email = user@myprivatebox.com
[push]
        default = simple
[core]
        editor = emacs
[includeIf "gitdir:~/CompanyAbc/"]
        path = ~/CompanyAbc/.gitconfig
[includeIf "gitdir:~/CompanyMno/"]
        path = ~/CompanyMno/.gitconfig

Set custom e-mail address in ~/CompanyAbc/.gitconfig

[user]
        email = user@company-abc.com

You can repeat this for other configurations like ~/CompanyMno/.gitconfig too.

Verification

Please execute command to verify new settings

user@mymac ~/CompanyAbc/some-repo/ (master) $ git config user.email

It should throw out something like

user@company-abc.com

Good luck with coding!

Comments 3 total

  • Alex V
    Alex VAug 5, 2022

    Thank you for this quick tip! This is very helpful for folks who are signing their commits as well!

  • Aravind A
    Aravind AAug 8, 2022

    This is not working for me

  • Jonatas de Oliveira Coêlho
    Jonatas de Oliveira CoêlhoNov 25, 2022

    Awesome! Helped a lot! Thx!

Add comment