Keep work and personal projects on one laptop and sooner or later a commit goes out under the wrong email address. I’d rather fix that once in my config than remember it in every repository, and git has a setting for it: a conditional include.
In your global ~/.gitconfig you add a block that only loads an extra file when the repository sits under a certain folder:
[user]
name = Your Name
email = [email protected]
[includeIf "gitdir:~/work/"]
path = ~/.gitconfig-work
And in ~/.gitconfig-work:
[user]
email = [email protected]
Every repository under ~/work now commits with the work address, everything else keeps the personal one. The trailing slash matters: git turns gitdir:~/work/ into ~/work/**, so it matches every repository in that folder and below it, and ~/ is replaced by your home directory.
To see which value wins, run this inside a work repository:
git config --show-origin user.email
It prints the file the value came from next to the value, so you know right away whether the include kicked in.
Order is the thing to watch. Git reads the included file at the spot where the includeIf block stands, and a later value overrides an earlier one. Put the block below your default [user] section, or the default address wins again. The condition also looks at where the .git directory is, so a work project you cloned into ~/Downloads won’t pick it up. The same trick works for more than email: a signing key or a different core.sshCommand per folder can go in that extra file too.
Further reading: git-config documentation: conditional includes
Going deeper: Guide: advanced tmux, a config that grows with you, formats, layouts and more