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.
First of all ensure you configure your git user globally using your companies email address. You can do that by using following command.
git config --global user.name "Marco Franssen" git config --global user.email "[email protected]"
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?
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.
$ tree -L 3 code ├── priv │ ├── blog │ ├── helm │ ├── kubernetes │ └── spire ├── spire ├── helm ├── work-project-a ├── work-project-b ├── work-project-c └── work-project-d 9 directories, 0 files
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.
git config --file ~/.gitconfig-private user.email [email protected]
This will result in the following
$ cat ~/.gitconfig-private [user] email = "[email protected]"
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
We can do that using following command.
git config --global includeIf."gitdir:~/code/priv/**/*".path "~/.gitconfig-private"
This will result in the following
~/.gitconfig file. (omitting here any other configs you might there)
$ cat ~/.gitconfig [user] name = "Marco Franssen" email = "[email protected]" [includeIf "gitdir:~/code/priv/**/*"] path = ~/.gitconfig-private
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).
$ cat ~/.gitconfig [user] name = "Marco Franssen" email = "[email protected]" [includeIf "gitdir:~/code/priv/**/*"] path = ~/.gitconfig-private $ cat ~/.gitconfig-private [user] email = "[email protected]"
For more information on conditional includes see the Git documentation.