The problem is pretty straight forward: I have a personal email and a work email and depending on the project I’m working on, I should commit as the identity relevant to that repo. So in a personal project, the git author should be damien@personalemail.com and within a work project, the git author should be damien@workemail.com.
Git config has the ability to include other config files based on the directory the repo is in outline in the git-config Includes. Let’s assume our ~/.gitconfig file looks something like this:
[user]
name = Damien Pontifex
email = damien@personalemail.com
Let’s assume our work repositories live in ~/work (or any subfolder from this directory), add to your ~/.gitconfig the following lines:
[includeIf "gitdir:~/work"]
path = ~/work/.gitconfig
Now over in ~/work we’ll add a new gitconfig file at ~/work/.gitconfig and it’s contents will be:
[user]
email = damien@workemail.com
The result is if I commit into any repository where it’s sitting as a child of ~/work the commit author will be damien@workemail.com and any other commit outside this folder will be damien@personalemail.com
To summarise, the final result for our gitconfig setup is:
~/.gitconfig
[user]
name = Damien Pontifex
email = damien@personalemail.com
[includeIf "gitdir:~/work"]
path = ~/work/.gitconfig
~/work/.gitconfig
[user]
email = damien@workemail.com