Guide to Adding Multiple Email to Your Existing GPG Key for Git
While adding my secondary email address to my GitHub account, I wanted to link all my new commits to this new email address. However, since I use GPG keys to sign my commits, my GPG key was connected to my previous email address. So, I had to create another GPG key for the new email address, and this approach worked.
But then I wondered: Is it possible to use the same GPG key for both email addresses? After some research, I discovered that it is indeed possible to add multiple email addresses to a single GPG key. In this blog post, I’ll explain how you can do it, too.
Prerequisite: I'm assuming you already have a GPG key added to your GitHub / GitLab account. If you don't, you can follow my articles on Generating and Adding GPG Keys to Your GitHub Account and How to Sign Your Git Commits with GPG Keys.
Retrieve Existing GPG Key:
First, let's retrieve the GPG key ID associated with the primary email address by running the following command:
gpg --list-secret-keys --keyid-format=long
This will list all the GPG keys on your system. From the list, copy the GPG key ID that is added to your GitHub/GitLab account.
For example, the output will look something like this:
sec rsa3072/A369A9BFD11F6F60 2024-03-14 [SC]
441F9580CF1B5238C3F0B2FCA369A9BFD11F6F60
uid [ultimate] John Doe (Example) <john.doe@example.com>
ssb rsa3072/F038B8C5AD80A86D 2024-03-14 [E]
Here, the GPG key ID is A369A9BFD11F6F60.
Add Email to GPG Key:
After successfully retrieving the GPG key ID, let's edit the key by running the following command:
gpg --edit-key <Your-GPG-Key-ID>
This will open the GPG command prompt interface. Then we'll use the adduid
command to add another username and email to the GPG key.
adduid
This will prompt you to enter the name, email address, and comment. Enter the required information and save it. This will update the existing GPG key with the new user's information. You can add more email addresses if needed.
Now, to double-check if the email has been added, let's run the following command:
gpg --list-secret-keys --keyid-format=long
Now the output will look something like this, showing both email addresses:
sec rsa3072/A369A9BFD11F6F60 2024-03-14 [SC]
441F9580CF1B5238C3F0B2FCA369A9BFD11F6F60
uid [ultimate] John Doe (Example) <john.doe@example.com>
[ultimate] Jane Doe (Example) <jane.doe@example.com>
ssb rsa3072/F038B8C5AD80A86D 2024-03-14 [E]
Now that we've successfully edited and saved the GPG key, let's retrieve it by running the following command:
gpg --armor --export <Your-GPG-Key-ID>
After successfully retrieving the key, we need to delete the old GPG key from GitHub (Settings -> SSH and GPG keys) and add the new one. After adding it, you will see both email addresses in the GPG key dashboard on GitHub. If any email address shows as unverified, it means it hasn't been added to your GitHub account yet.
I hope this article helps you add additional email addresses to your existing GPG key and work with signing via GPG keys with the new email address. Please share your feedback and any suggestions for improvements. Thank you. Have a good day.
Subscribe to my newsletter
Read articles from Dushmanta directly inside your inbox. Subscribe to the newsletter, and don't miss out.
Written by
Dushmanta
Dushmanta
Passionate Software Engineer