Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

manage 2 git users gpg key and choose gpg sign per user

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?

like image 267
slifer2015 Avatar asked Sep 24 '18 12:09

slifer2015


People also ask

How do I get a GPG signing key?

Run the command gpg --armor --export KEY-ID to get your GPG public key and add it to your repository manager. These keys are then used to generate badges to indicate if your commits are verified.

How do I switch users in git?

gitconfig file into the . git folder (and rename it to "config") and just change the lines you want to change (probably github. user and github. token) or you create a new file with just the two lines in it.


1 Answers

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 everytime 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      = [email protected]
  signingkey = DEADBEEF

# config.gitlab
[user]
  name       = Chakrit
  email      = [email protected]
  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.

like image 157
chakrit Avatar answered Sep 19 '22 13:09

chakrit