Git tip for setting different emails for different folders

Want to use one email for personal/study repositories and another for work projects without manual configuration each time? Learn how to set up Conditional Includes in your .gitconfig file for seamless email management across different project folders.

By Maurício Cantú·Jan 4, 2025

This is useful if you want to use one email for your personal/study repositories and another email for work projects, without having to manually configure the desired email for each new repository.

Given the following folder structure in the system:

  • ~/.gitconfig (main configuration file in the system home)
  • ~/Projects/Personal
  • ~/Projects/Work

In the main '.gitconfig' file, we have the default settings for all repositories. Example:

[user]
name = Yor Name
email = yourpersonal@email.com
other configs...

To configure the email to be used in the repositories of the 'Projects/Work' folder, we add the following snippet to the main .gitconfig file:

[includeIf "gitdir:~/Projects/Work/"]
path = ~/Projects/Work/.gitconfig

Now, in the 'Projects/Work' folder, we create a .gitconfig file with the following content:

[user]
email = yourname@company.com

Done! Now, all repositories inside the 'Work' folder will use your work email, while the repositories outside will use the default email defined in the main .gitconfig file.

This feature is called Conditional Include. Check the official documentation.

Any configuration beyond the email also works!

Thanks for reading and enjoy!