Override Git's User Email by Folder

Never forget to set your work email after cloning a work Git repository again!

When using a single computer for multiple categories of development (personal, work, etc), there is often a need to set a specific user.email value for each repository cloned depending on the use. For example, it’d make sense to use your work email for repositories cloned for work while you’d use your personal email for you own projects.

Without the following setup, it’s very easy to forget to override your user.email at a repository level and end up committing to one or the other with the wrong email attached. Though not the end of the world, I can imagine your company, coworkers, and security department would prefer to see your work email attached to all commits instead of a random Gmail account.

With the following in place, you’ll never forget to have the correct user.email set again!

Note: The following only works if you split up your repository clones within dedicated folders…which is probably better than mixing professional and personal repository together anyways.

.
├── personal
│   ├── repo1
│   └── repo2
└── work
    ├── repo1
    └── repo2

In your main ~/.gitconfig:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
; Set your desired default name and email
[user]
    name = Bob Loblaw
    email = [email protected]

; Override your user info, and anything else, depending on the folder you're
; currently in
; It's important this comes after anything you're overriding
[includeIf "gitdir:~/Development/work/**"]
    path = ~/.gitconfig_work.inc

Now in ~/.gitconfig_work.inc you can set your work specific information

1
2
3
[user]
    name = Bob Loblaw
    email = [email protected]

With those configs, now any Git interaction within ~/Development/work/** will be done with user.email set to [email protected]. No need for any local Git config overrides ever again to set common settings for work vs personal repositories.

See also