One Git email for each projects folder
Published on 4 min read
Use includeIf in ~/.gitconfig so each projects folder gets its own Git email, check the value with --show-origin and learn when a local config wins.
Cloning a work project and forgetting to change the Git email is easy, especially when personal and professional projects share the same machine. The commit works normally, and the wrong address only shows up later, in history that has already been pushed.
Since I keep these projects in separate directories, I use includeIf to tie a configuration to the work folder. That way, a new repository inside it gets the right identity without relying on a git config user.email after every clone.
What changes when I enter the folder
Picture two repositories, one in ~/Work/pessoal/app and another in ~/Work/empresa/app. I want pessoal@example.com in the first and trabalho@example.com in the second, keeping the same name in both. The paths and addresses in this example are fictional.
Git reads your global configuration and, when it finds a condition that matches the repository, it also reads the file that condition points to. That file can change only the email, preserving the name and the other options that were already set.

The image gathers outputs from a run in example repositories, with the global configuration isolated from my personal one. The directories were shortened to the names used in the text; the third case shows why it is still worth checking an old local configuration.
Two configuration files
In ~/.gitconfig, the personal email stays as the default and the company rule comes after it. Adapt the addresses and add the include to the configuration you already use, preserving the other options:
[user]
name = Your Name
email = pessoal@example.com
[includeIf "gitdir:~/Work/empresa/"]
path = ~/.gitconfig-empresa
~/.gitconfig-empresa only needs the value that changes:
[user]
email = trabalho@example.com
With that order, the email from the included file replaces the default when the repository is inside ~/Work/empresa/. The trailing slash makes the pattern reach subdirectories, so ~/Work/empresa/equipe/api also gets the configuration.
The personal default still applies outside that folder. If you prefer each group to have its own rule, you can create a second include and a file for the personal projects; the mechanism is the same.
Check before creating the commit
To try it without creating commits, initialize two empty repositories in the example directories:
mkdir -p ~/Work/pessoal/app ~/Work/empresa/app
git init ~/Work/pessoal/app
git init ~/Work/empresa/app
git -C ~/Work/pessoal/app config --get user.email
# pessoal@example.com
git -C ~/Work/empresa/app config --get user.email
# trabalho@example.com
If the result is different, check where the value comes from inside the affected repository:
git config --show-origin --get user.email
git config --show-origin --get user.name
--show-origin shows the file responsible for the effective value, which helps tell a rule that did not match the path apart from a local configuration that overrode it. Since the company file only changes the email, the name keeps coming from the global file.
When a local config wins
A repository that already has user.email in .git/config keeps that value even after the include is created. To reproduce this case in the example repository:
git -C ~/Work/empresa/app config --local user.email outro@example.com
git -C ~/Work/empresa/app config --show-scope --get user.email
# local outro@example.com
If that exception is not intentional, remove the local value and check again:
git -C ~/Work/empresa/app config --local --unset user.email
git -C ~/Work/empresa/app config --get user.email
# trabalho@example.com
The same check deserves attention in linked worktrees, because gitdir considers the Git metadata directory, which may live in the main checkout, not the folder where you are editing files. You can check the path with git rev-parse --absolute-git-dir.
The include sets up the identity for the next commits and removes a repetitive choice when you organize projects by folder. It does not change the address on existing commits or the account used to authenticate with the remote, which remain separate matters.
Reference
The Git documentation on conditional includes details the directory patterns and other available conditions.