Automatically keeping Git identities distinct between accounts
I’ve ended up with multiple GitHub identities. One for personal use, another for SEP use, and still more for many of our clients. Leaning on Git conditional includes, and a bit of manual work upfront, my GitHub identities can be “automatically” kept distinct.
Like many people, I’ve setup a global Git configuration for my name and email address. That works great for ensuring that when I push commits to GitHub they link to my main GitHub account. But, that global configuration can get in the way.
In the past, I’ve cloned repositories from each of the different accounts. Then I would diligently set the user.name and user.email fields for each repository. While that works, it’s very manual and easy to miss.
That’s where conditional includes come into play. I’ve setup a collection of .gitconfig files. My “default” setup goes into the global configuration. Then I have separate files for each GitHub identity I want to maintain.
The files look something like:
[user] name = Aaron email = firstname.lastname@example.org
[user] name = Aaron Alexander email = email@example.com
[user] name = Alexander, Aaron email = firstname.lastname@example.org
Then the main .gitconfig file needs some changes to conditionally apply the different files.
[includeIf "gitdir:~/Projects/Personal/"] path = .gitconfig-personal [includeIf "gitdir:~/Projects/Work/"] path = .gitconfig-work [includeIf "gitdir:~/Projects/Work/Client-A/"] path = .gitconfig-client-a
Those conditional includes map to a folder structure like:
~/Projects ~/Projects/Personal ~/Projects/Work ~/Projects/Work/Client-A
With those changes to the .gitconfig made, my commits “automatically” end up being associated with the correct email address (and thus GitHub account).
Having just migrated to a new computer, I’ve found the above approach nice in removing some manual setup I’d been used to doing in the past. If you have multiple GitHub identities to manage, I’d recommend giving it a try.