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.
Or execute the following statement to modify the configuration (both methods are essentially the same).
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.
Open the global configuration .gitconfig
file and add the directory configuration.
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 config
s.
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.
Reference
https://git-scm.com/docs/git-config#_conditional_includes
https://stackoverflow.com/questions/43919191/git-2-13-conditional-config-on-windows