How to have different git configs for different directories

Let's say you have a different git account for workplace repos, and personal repos. Instead of specifying which account to use for each repo, you can just specify which account to use for all repos in a specific directory. In this example we have our work repos at ~/work-repos/ and personal repos anywhere else.

First create new config file for your work git account: touch .gitconfig-work

// .gitconfig-work
    email = your.work@email.com
    name = Hannes Kinnunen

Then we need to tell git to use .gitconfig-work when we're inside ~/work-repos/. This is easy:

// .gitconfig
    email = your.personal@email.com
    name = Hannes Kinnunen
[includeIf "gitdir:~/work-repos/"]
   path = ~/.gitconfig-work

That's it! You can check that it works by creating repo inside ~/work-repos/ and calling git config user.email which should reply with your work email.


- 3 toasts