How To Use Different Git Configs
Optimize Git configs with conditional includes for multiple projects, managing user profiles and streamlining commit associations.
A lot of us are coding for an employee and privately or as a freelancer for multiple companies. You might want to associate commits for the different projects with a different user (e.g. email) or sign them differently (I think you should).
While you could configure Git per repository it becomes pretty cumbersome. Instead, configuring Git for a subset of all of your local repositories is much more handy. This is what I will be going to show you in this post.
Create Your Default, Global .gitconfig
First of all, we create our default, global .gitconfig
by typing git config --global -e
into your terminal. Let us keep this example simple and only config the name
and email
for our git user
:
[user]
name = Jannik Wempe
email = jannik@wempe.dev
.gitconfig
on MacOS ~/.gitconfig
(in you home folder).With that config, every one of your commits will be associated with the shown name
and email
β no matter where your repository is located on your machine.
Overriding Git Configuration For Some Repositories
In order to be able to override your Git configuration for multiple repositories at once we have to group them in a folder. I use a ~/code
folder that looks like this:
.
βββ work
βββ private
I want to associate my commits in repositories that are located in ~/code/work
with a different email address. Let us create a separate Git configuration for that: touch ~/.gitconfig-work
. This is what that file looks like:
[user]
email = jannik@other.com
We now have ~/.gitconfig
and ~/.gitconfig-work
in our home directory. But how to tell Git to use a different config for all repositories within ~/code/work
?
By changing your ~/.gitconfig
to this:
[user]
name = Jannik Wempe
email = jannik@wempe.dev
[includeIf "gitdir/i:~/code/work/"]
path = ~/.gitconfig-work
gitdir/i
is a condition telling Git to include the file at the given path
if the Git repository is located in any sub-directory of ~/code/work/
(ending the path with /
automatically adds /**
and thus matching all sub-directories). The /i
part is configures the matching to be case-insensitive.
The
include
andincludeIf
sections allow you to include config directives from another source. These sections behave identically to each other with the exception thatincludeIf
sections may be ignored if their condition does not evaluate to true; see "Conditional includes" below.
β Git - git-config Documentation
onbranch
. Check out the documentation for details.The content of ~/.gitconfig-work
will be inserted at the exact position where it is mentioned. That is why it will override previous configurations with the same (and why the includes should be at the end of the file). This way you could effectively append any configuration to your default .gitconfig
β not just the user
property.
With this config, any new commit in any repository located within ~/code/work
will be associated with the email configured in .gitconfig-work
. Et voilΓ‘, we have overridden the Git configuration for multiple Git repositories at once.
Conclusion
You can stitch together Git configuration files by leveraging Include
and IncludeIf
. The conditional IncludeIf
allows us to conditionally overwrite certain configurations.