When using git on a daily basis, there is usually a global configuration file .gitconfig that all repo’s will use by default. If you need to configure a particular repo, you just need to modify the .git/config file in the repo. However, if you need to change more repo’s, it’s easy to forget and commit the wrong author’s commit.

Demo

For example, if you want to use different user.name and user.email for your company repo and your personal repo, you can simply modify the configuration in .git/config in your personal repo.

1
2
3
[user]
    name = name1
    email = name1@example.com

Or execute the following statement to modify the configuration (both methods are essentially the same).

1
2
git config user.name name1
git config user.email name1@example.com

This will cover the global configuration, but with a large number of repo’s, this method becomes inefficient.

Solution

Personally, I would use different folders to distinguish between company and personal repo’s, so I can use git conditional include to set different repo’s in different directories with different git config. Let’s assume that the company repo is in the work directory.

Create a new .gitconfig-work with the following contents.

1
2
3
[user]
    name = name2
    email = name2@example.com

Open the global configuration .gitconfig file and add the directory configuration.

1
2
[includeIf "gitdir:~/work/"]
    path = .gitconfig-work

This way new repo’s under work will use the configuration in .gitconfig-work to override the global configuration, so that different directories use different git configs.

Tip

For existing repo’s in the work directory, you can run the following command to see if the configuration in .gitconfig-work is in effect.

1
2
git config --show-origin --get user.name
git config --show-origin --get xxxx

Reference

  • https://git-scm.com/docs/git-config#_conditional_includes
  • https://stackoverflow.com/questions/43919191/git-2-13-conditional-config-on-windows