Globally configure multiple git commit emails
Marco Franssen /
4 min read • 746 words
Have you ever been struggling to commit with the right email address on different repositories? It happened to me many times in the past, but for a couple of years I'm now using an approach that prevents me from making that mistake. E.g. when working on your work related machine, I'm pretty often also working on Opensource in my spare time, to build my own skills, and simply because I believe in the cause of Opensource. Also during work time I'm also sometimes contributing fixes back to Opensource projects we are using, for those times I want to commit using my work email address when committing. You can imagine it is a hassle to continuously think about using the correct email address when committing. In this blog I will be sharing a really simple solution to that.
I will assume you are configuring this on your work machine, in case you would like to apply similar on your personal machine, ensure to do the opposite of my suggestions.
Configure your git user globally
First of all ensure you configure your git user globally using your companies email address. You can do that by using following command.
Using this setting all repositories you clone or create on this machine will use this user information to commit. So what about working on private projects from this machine?
Configure alternative settings for private projects
To work on private projects I came up with a folder based approach that allows me to configure git in the global configuration file ~/.gitconfig
to use different user information for projects in a specific folder. In my case I have all my code in a ~/code
folder, and for personal projects I choose to put them in ~/code/priv
. See following terminal output for an example.
Now when working in my private time I work on the repositories from the ~/code/priv/…
folders. When working in my company time I work on the projects outside of the ~/code/priv
folder. This also allows me to easily contribute to opensource projects using my company email address. Only downside is that some projects are cloned twice. I did try with symbolic links to get rid of this downside, but unfortunately that didn't seem to work.
Now to enable globally to commit with my private email address when working within the ~/code/priv/…
folders I create another git config that I can conditionally include in the global config.
This will result in the following ~/.gitconfig-private
file.
You could also put other overrides in this configuration file. E.g. you want to use a different GPG key for private contributions.
Now this is where the magic is going to happen. In our global config we want to add the conditional statement that includes our new ~/.gitconfig-private
that overrides the global settings when in a particular folder on your system using the ones defined in ~/.gitconfig-private
.
We can do that using following command.
This will result in the following ~/.gitconfig
file. (omitting here any other configs you might there)
Recap
Using the following global git configuration we will by default commit all our code using our work email address except for the code located within ~/code/priv
. If you wanted to do the same on your personal machine you might want to do the inverse (~/code/work
and then configure globally your private email and override with your work email).
This is how the entire config looks like (omitting any other configurations you might have applied).
For more information on conditional includes see the Git documentation.