The best way to do this since git 2.13 is to use Conditional includes.
An example (copied from an answer here):
Global config ~/.gitconfig
[user]
name = John Doe
email = john@doe.tld
[includeIf "gitdir:~/work/**"]
path = ~/work/.gitconfig
Work specific config ~/work/.gitconfig
[user]
email = john.doe@company.tld