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!