Multiple Git identities
Everything is code, and code is everywhere. For me, this means I put more and more stuff into version control. Whether it is infrastructure descriptions, documentation, software or this blog, I always store it in a Git repository. But sometimes this gets dirty, because you accidentally forget to change your Git user details.
I’ve pushed code for my blog using the email address I have at a customer. This is not a big problem, it’s a private repository anyway. Worse, I’ve pushed code for a client project using my private email address. I typically don’t want them to know my private email address.
The typical way to fix that is edit .git/config for every repository:
[user]
name = Maarten Mulders
email = maarten@example.org
Now that I’m also an Apache Maven committer, I’d prefer to have all my commits their from my @apache.org email address. But Maven consists of several hundreds of repositories, when you include all the plugins as well. How to solve this for once and for all?
includeIf
to the rescue!
My ~/Code folder is pretty structured:
- ~/Code/open-source/maven/ contains the clones of Apache Mavens Git repositories.
- ~/Code/private contains my pet projects.
- ~/Code/work is where my company work stays.
- etc…
This structure comes to use when solving this problem. Starting with Git 2.13, your global Git configuration can conditionally include additional configuration!
Look at my ~/.gitconfig:
[user]
name = Maarten Mulders
email = maarten@example.org
[includeIf "gitdir/i:/Users/maarten/Code/open-source/maven/"]
path = /Users/maarten/Code/open-source/maven/gitconfig
In ~/Code/open-source/maven/ I created an additional gitconfig file:
[user]
name = Maarten Mulders
email = notreallymaarten@apache.org
signingkey = YYYYYYYYYYYYYYYY
Whenever I work with Git in a repository under ~/Code/open-source/maven/, it will now use my @apache.org email address. Neat!
Of course, I create a similar files in ~/Code/work/, ~/Code/private/… Say goodbye to using the wrong email address, signing key or whatever in your commit messages!