Skip to content

Martin Belev

How to configure Git with multiple emails?

Git2 min read

git config

Most probably, everyone went through the simplest form of Git setup when we started using it. What I mean is configuring our username and email that is going to be used with Git. E.g. git config --global user.email "<your-email-address>" which will add a line into our .gitconfig file and it will be used throughout all of our Git repositories.

This is perfect when we haven't started a job yet but when we do, it's almost certain that the company will require from us to use our company email for company projects. However, it makes perfect sense that we would like to use our personal email for personal projects. Today we are going to see how we can achieve this and do it only once without the need of repeating the process.

Link to this heading
Simple .gitconfig setup

.gitconfig
1[user]
2 name = Martin Belev
3 email = <your-personal-email>

By using the ☝️ config, we are going to use the same username and email in every one of our Git repositories. We will need to make a change which will use one email or another based on some condition. Not as a surprise I guess, there is such functionality in Git and it's called conditional includes.

Link to this heading
Multiple emails .gitconfig setup

We are going to use conditional includes which is providing us with a way to include another config based on a certain condition.

.gitconfig
1[includeIf "gitdir:~/personal-work/"]
2 path = .gitconfig-personal
3[includeIf "gitdir:~/work/"]
4 path = .gitconfig-work
5
6[user]
7 name = Martin Belev

I like to keep things organized and keep my personal projects inside personal-work directory and my company work-related projects inside work directory. gitdir is a keyword which is used to match the location of the .git directory to the provided glob pattern. If so, the include condition will be true.

The content in the two additional config files is simply the email Git config:

.gitconfig-personal
1[user]
2 email = <your-personal-email>
.gitconfig-work
1[user]
2 email = <your-company-email>

If you would like to use your personal email always except for the cases with your work-related projects then you can leave the personal email in the .gitconfig and include the additional configuration for your work-related email at the end of the file.

.gitconfig
1[user]
2 name = Martin Belev
3 email = <your-personal-email>
4
5...
6
7[includeIf "gitdir:~/work/"]
8 path = .gitconfig-work

I am keeping the files at the root level to have them side by side, but this is not mandatory. If you think it's better structured to put the additional config somewhere else, it doesn't really matter and it should be working.

Link to this heading
Bonus

As a small bonus, I would like to share my current .gitconfig that I am using.

1[includeIf "gitdir:~/personal-work/"]
2 path = .gitconfig-personal
3[includeIf "gitdir:~/work/"]
4 path = .gitconfig-work
5
6[user]
7 name = Martin Belev
8[push]
9 default = simple
10[core]
11 editor = vim
12 excludesfile = /Users/belevmar/.gitignore_global
13 ignorecase = false
14[alias]
15 df = diff
16 st = status
17 co = commit
18 p = push
19 ch = checkout
20 chb = checkout -b
21 br = branch
22 dlb = branch -d
23 dlbh = branch -D
24 drb = push origin --delete
25 lg = log --graph --branches --remotes --pretty=format:'%C(red)%h%Creset -%C(yellow)%d%Creset %s %C(green)(%cr) %C(bold blue)<%an>%Creset' --abbrev-commit --date=relative
26 pu = pull
27 pur = pull --rebase
28 puru = pull upstream master --rebase
29[credential]
30 helper = cache
31[rerere]
32 enabled = true
33[pull]
34 rebase = true

Link to this heading
Conclusion

We have seen how to configure Git to use 2 emails based on the location of the .git folder by loading additional configuration files using the feature of Git called conditional includes. If you saw something new that you don't know about in my .gitconfig file, I am more than happy to share more information about it, so don't be shy and reach out to me 🙃.


Thank you for reading this to the end 🙌 . If you enjoyed it and learned something new, support me by clicking the share button below to reach more people and/or give me a follow on Twitter where we can catch up. I am sharing some other tips, articles, and things I learn there.
If you didn't like the article or you have an idea for improvement, please reach out to me on Twitter and drop me a DM with feedback so I can improve and provide better content in the future 💪.

© 2021 by Martin Belev. All rights reserved.