I very like the way of Micah Henning in his article (see Setting Up Git Identities) on this subject. The fact that he apply and force the identity to each repository created/cloned is a good way not to forget to set this up each time.
Basic git configuration
Unset current user config in git:
$ git config --global --unset user.name
$ git config --global --unset user.email
$ git config --global --unset user.signingkey
Force identity configuration on each new local repository:
$ git config --global user.useConfigOnly true
Create Git alias for identity
command, we will use later:
$ git config --global alias.identity '! git config user.name "$(git config user.$1.name)"; git config user.email "$(git config user.$1.email)"; git config user.signingkey "$(git config user.$1.signingkey)"; :'
Identities creation
Create an identity with GPG (use gpg
or gpg2
depending on what you got on your system). Repeat next steps for each identities you want to use.
Note: [keyid]
here is the identifier of created secret key.
Example here:
sec rsa4096/8A5C011E4CE081A5 2020-06-09 [SC] [expires: 2021-06-09]
CCC470AE787C057557F421488C4C951E4CE081A5
uid [ultimate] Your Name <youremail@domain>
ssb rsa4096/1EA965889861C1C0 2020-06-09 [E] [expires: 2021-06-09]
The 8A5C011E4CE081A5
part after sec rsa4096/
is the identifier of key.
$ gpg --full-gen-key
$ gpg --list-secret-keys --keyid-format LONG <youremail@domain>
$ gpg --armor --export [keyid]
Copy the public key block and add it to your GitHub/GitProviderOfChoice settings as a GPG key.
Add identity to Git config. Also repeat this for each identity you want to add:
Note: here I use gitlab
to name my identity, but from your question it can be anything, ex: gitolite
or github
, work
, etc.
$ git config --global user.gitlab.name "Your Name"
$ git config --global user.gitlab.email "youremail@domain"
$ git config --global user.gitlab.signingkey [keyid]
Setup identity for a repository
If a new repo has no identity associated, an error will appear on commit, reminding you to set it.
*** Please tell me who you are.
## parts of message skipped ##
fatal: no email was given and auto-detection is disabled
Specify the identity you want on a new repository:
$ git identity gitlab
You're now ready to commit with the gitlab identity.