This method involves adding some conditions to global git config. Then within each git repo, it will automatically figure out which email to use based on the path.
By default, the global git config looked something like this for me.
# ~/.gitconfig
[user]
email = personal@email.com
name = Hao Dong1. Rename “.gitconfig” to “.gitconfig-personal”
Rename the global config to ~/.gitconfig-personal using the following command. It will now be our new config for any personal projects.
mv .gitconfig .gitconfig-personalDon’t worry, we will add the global config back later.
2. Create “.gitconfig-work”
Create the new config file for work.
touch ~/.gitconfig-workThen add the following into the config file.
# ~/.gitconfig-work
[user]
email = work@email.com
name = Hao Dong3. Create a new “.gitconfig”
Add a new global .gitconfig file, this will point to the other two custom config files.
touch ~/.gitconfigThen add the following into the global config file.
# ~/.gitconfig
[includeIf "gitdir:~/work/**"]
path = .gitconfig-work
[includeIf "gitdir:~/personal/**"]
path = .gitconfig-personal