TL;DR: since Git 2.13 there’s an easy way to differentiate between personal and work Git credentials. More on that below.
Just like many IT professionals I have a Github account which I use both for my personal and day job projects.
Github allows to setup multiple email addresses in your account and even custom notification routing so that notifications for work repositories go to your work email. Here’s a screenshot from notifications settings page:
Since I’m registered in Github using my personal email address (wheleph@gmail.com) and personal nickname (wheleph), my global ~/.gitconfig
is setup accordingly:
# This is Git's per-user configuration file.
[user]
name = wheleph
email = wheleph@gmail.com
This means that every time I do git commit
this is what other people see:
Author: wheleph <wheleph@gmail.com>
That’s fine for my personal repos but it’s not ideal for my work repos. For those repos I’d like to have something like:
Author: Volodymyr Sobotovych <volodymyr.sobotovych@dundermifflinpaper.biz>
Of course it’s possible to have individual .git/config
for every work repo I have locally. This is tedious and error-prone but for older Git releases it was the only way to go.
Fortunately since Git 2.13 I can override any settings (including user name and email) for all repositories located in a particular directory. Here’s how my global .gitconfig
looks like now:
~$ cat ~/.gitconfig
# This is Git's per-user configuration file.
[user]
name = wheleph
email = wheleph@gmail.com
[core]
excludesfile = /Users/wheleph/.gitignore
[pull]
ff = only
[includeIf "gitdir:~/dm_repos/"]
path = ~/dm_repos/.gitconfig
I keep all work repositories in directory ~/dm_repos
and also there I have this “override” file:
~$ cat ~/dm_repos/.gitconfig
[user]
name = Volodymyr Sobotovych
email = volodymyr.sobotovych@dundermifflinpaper.biz
With this approach all I have to care about is keeping my work repos in a given directory and Git will take care of using proper user name and email for my commits!
BTW Intellij Idea also respects this setting.
References: