— Git — 2 min read
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.
1[user]2 name = Martin Belev3 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.
We are going to use conditional includes which is providing us with a way to include another config based on a certain condition.
1[includeIf "gitdir:~/personal-work/"]2 path = .gitconfig-personal3[includeIf "gitdir:~/work/"]4 path = .gitconfig-work56[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
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:
1[user]2 email = <your-personal-email>
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.
1[user]2 name = Martin Belev3 email = <your-personal-email>45...67[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.
As a small bonus, I would like to share my current
.gitconfig that I am using.
1[includeIf "gitdir:~/personal-work/"]2 path = .gitconfig-personal3[includeIf "gitdir:~/work/"]4 path = .gitconfig-work56[user]7 name = Martin Belev8[push]9 default = simple10[core]11 editor = vim12 excludesfile = /Users/belevmar/.gitignore_global13 ignorecase = false14[alias]15 df = diff16 st = status17 co = commit18 p = push19 ch = checkout20 chb = checkout -b21 br = branch22 dlb = branch -d23 dlbh = branch -D24 drb = push origin --delete25 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=relative26 pu = pull27 pur = pull --rebase28 puru = pull upstream master --rebase29[credential]30 helper = cache31[rerere]32 enabled = true33[pull]34 rebase = true
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 don't like the article, please leave a comment below or write me a DM with feedback what you don't like so I can improve and provide better content in the future 💪.