Maintaining Different Git Identities
Max Jul 02, 2017
I like to have separate "identities" for my private and work stuff when using Git: Commits at work should be authored with my work email and commits in private projects with my private account. Until now I would always configure this per repository as soon as I noticed a commit done by the wrong email.
As I was setting up my new computer and edited the global
.gitconfig, I wondered if there is a better way to keep this separate. Turns out there is one: With the release of version 2.13, Git introduced "Conditional Includes".
With these includes I can set a specific gitconfig-file to be include for all repositories within a specific location. As I store all my work projects within the folder
~/Work, I set the default user-config to be my private one and include the work-specific configfile for all Git repositories within that location.
# ~/.gitconfig [user] name = Firstname Lastname email = <private email address> [includeIf "gitdir:~/Work/"] path = .gitconfig-work
# ~/.gitconfig-work [user] name = Lastname, Firstname email = <work email address>
The risk of being annoyed by wrongly associated commits is vastly smaller now — until I start checking out work projects to ~/Desktop/tmp for minor fixes.
To check your configuration, make sure to be in a directory which is tracked by Git. Non-Git directories will always show the default configuration:
$ cd ~/dev/justcurious $ git config user.email <private email address> $ cd ~/Work/projectA $ git config user.email <work email address> $ cd ~/Work/not-a-repo $ git config user.email <private email address>