
Git with Multiple Identities
Using Git with multiple identities on the same client and signing commits with the matching GPG keys.
If you regularly work with different Git identities, you have surely pushed commits with the “wrong” user once or twice.
In my current project at the customer “SomeCustomer” I work with a dedicated GitLab user thomas.hartl@somecustomer.com, while at evoila I use my GitHub company account thartl@evoila.com. So that I don’t have to select the right user on every push, I keep all projects locally in different directories:
/Projects
├─ evoila
│ ├─ Repository X
│ └─ Repository Y
└─ SomeCustomer
└─ Repository Z
This directory structure allows me to build a dynamic Git config with conditional includes that automatically picks the “right” user for each repository. To do so, I create three configuration files in my home directory:
- My .gitconfig contains the Git “base configuration” and two includeIf directives that, based on the filter
gitdir:<directory>, include two different additional configuration files.
# .gitconfig
[includeIf "gitdir:/Projects/evoila/"]
path = ~/.gitconfig.evoila
[includeIf "gitdir:/Projects/SomeCustomer/"]
path = ~/.gitconfig.somecustomer
- The .gitconfig.evoila file contains my evoila-specific settings, including my evoila user and the associated GPG key.
# .gitconfig.evoila
[user]
name = Tom Hartl
email = thartl@evoila.com
signingkey = MNOPQRSTUVW98765
- Analogously, the .gitconfig.somecustomer file contains the user and GPG key for the customer’s GitLab instance.
# .gitconfig.somecustomer
[user]
name = Thomas Hartl
email = thomas.hartl@somecustomer.com
signingkey = ABCDEFGHIJK12345
With this dynamic configuration I no longer have to think about which user I push to which repo with, since Git automatically picks the correct user based on the directory I’m working in. I can also specify different GPG keys for signing commits.
Sources
- Git-SCM: Git-Config