As of Git 2.13 you can use an includeIf
in your gitconfig to include a file with a different configuration based on the path of the repository where you are running your git commands.
Since a new enough Git comes with Ubuntu 18.04 I've been using this in my ~/.gitconfig
quite happily.
[include]
path = ~/.gitconfig.alias # I like to keep global aliases separate
path = ~/.gitconfig.defaultusername # can maybe leave values unset/empty to get warned if a below path didn't match
# If using multiple identities can use per path user/email
# The trailing / is VERY important, git won't apply the config to subdirectories without it
[includeIf "gitdir:~/projects/azure/"]
path = ~/.gitconfig.azure # user.name and user.email for Azure
[includeIf "gitdir:~/projects/gitlab/"]
path = ~/.gitconfig.gitlab # user.name and user.email for GitLab
[includeIf "gitdir:~/projects/foss/"]
path = ~/.gitconfig.github # user.name and user.email for GitHub
https://motowilliams.com/conditional-includes-for-git-config#disqus_thread
To use Git 2.13 you will either need to add a PPA (Ubuntu older than 18.04/Debian) or download the binaries and install (Windows/other Linux).