How to configure Git with multiple emails?
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.
Simple .gitconfig setup
# .gitconfig[user]
name = Martin Belev
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.
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[includeIf "gitdir:~/personal-work/"]
path = .gitconfig-personal
[includeIf "gitdir:~/work/"]
path = .gitconfig-work
[user]
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
[user]
email = <your-personal-email># .gitconfig-work
[user]
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[user]
name = Martin Belev
email = <your-personal-email>
...
[includeIf "gitdir:~/work/"]
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.
Bonus
As a small bonus, I would like to share my current .gitconfig
that I am using.
# .gitconfig[includeIf "gitdir:~/personal-work/"]
path = .gitconfig-personal
[includeIf "gitdir:~/work/"]
path = .gitconfig-work
[user]
name = Martin Belev
[push]
default = simple
[core]
editor = vim
excludesfile = /Users/belevmar/.gitignore_global
ignorecase = false
[alias]
df = diff
st = status
co = commit
p = push
ch = checkout
chb = checkout -b
br = branch
dlb = branch -d
dlbh = branch -D
drb = push origin --delete
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
pu = pull
pur = pull --rebase
puru = pull upstream master --rebase
[credential]
helper = cache
[rerere]
enabled = true
[pull]
rebase = true
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 sharing the article 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, or write a comment with feedback so I can improve and provide better content in the future 💪.
Originally published at https://belev.dev.