16

I have 1 github user and another gitlab user, and I have created 1 gpg key for each because my email address differs.

The problem is I have to execute git config --global user.signingkey everytime I want to commit to different git repos.

Is there a way I can manage my gpg keys per git user?

CodeBoyCode
  • 2,227
  • 12
  • 29
slifer2015
  • 682
  • 6
  • 12
  • Possible duplicate of [Specify Git identity based on SSH host or identity](https://stackoverflow.com/questions/52291450/specify-git-identity-based-on-ssh-host-or-identity) – Timothy Truckle Sep 24 '18 at 12:51

3 Answers3

30

I have the same situation but with splitting of work/personal accounts. And I have a lot of repositories but I don't want to run git config every time I clone something new.

I have written a blog post about it. A way to do this automatically is to use the includeIf directive provided by git. You can read more about it from the Conditional Include section in git manual.


There's a small requirement tho, you need to be able to tell apart github repositories from your GitLab repositories by a component in your path (for example, put GitHub clones in ~/github and Gitlab clones in ~/gitlab)

Then, basically, split the signing key configuration into two files:

# config.github
[user]
  name       = Chakrit
  email      = github@example.com
  signingkey = DEADBEEF

# config.gitlab
[user]
  name       = Chakrit
  email      = gitlab@example.com
  signingkey = BADC0FFEE

And then in your main ~/.config/git/config configuration file, use the includeIf gitdir: directive to match and include different files based on your WD:

# when working with github
[includeIf "gitdir:**/github/**/.git"]
  path = config.github

# when working with gitlab
[includeIf "gitdir:**/gitlab/**/.git"]
  path = config.gitlab

Then all repos in your ~/github folder will automatically use your GitHub key and repos in your ~/gitlab folder will use your GitLab keys.

Abraham
  • 8,525
  • 5
  • 47
  • 53
chakrit
  • 61,017
  • 25
  • 133
  • 162
5

Just remove all user.signingkey settings from your repo and your global settings.

git will decide to use a key ingpg with a matching identity for the user.email setting from your repo.

Casper
  • 51
  • 1
  • 1
  • Can you share the official documentations which states this in a more elaborate manner? – Jarmos Nov 17 '22 at 07:31
  • 1
    @Jarmos: [user.signingKey](https://git-scm.com/docs/git-config#Documentation/git-config.txt-usersigningKey): "If git-tag or git-commit is not selecting the key you want it to automatically when creating a signed tag or commit, you can override the default selection with this variable. [...]" [gpg.ssh.defaultKeyCommand](https://git-scm.com/docs/git-config#Documentation/git-config.txt-gpgsshdefaultKeyCommand): "This command that will be run when user.signingkey is not set and a ssh signature is requested. [...]" – andthum Jan 31 '23 at 09:10
  • That won't work if your login account UPN is not the same as the email used for signing key. – Igor Levicki Jun 03 '23 at 14:19
4

The signingkey may be set per-repository, just execute: git config user.signingkey from within a checked out repository. This command sets the configuration in the repositor's .git/config. The --global options causes the config to be written into ~/.gitconfig where it becomes the default for repositories that do not have a local value set.

More information can be found in git-config's man page in the user.signingKey section.

You may also get better results by also setting user.email to the email associated with the repository.

james
  • 41
  • 3