Create a GPG Key
Why Use a GPG Key?
The SSH key we made in the previous post was meant to encript your code changes between your local repositories and your remote repositories. A GPG key is similar in that it encrypts and signs your commits in your local repository so that your remote repository can verify that the commits were made by a trusted individual. Like the SSH key, you will have to generate a private and public key and add the public key to your remote repository hosting site of choice. This tutorial will focus on GitHub, but the same intructions will easily translate to other sites. This tutorial is based on those found here.
Generate a new GPG Key
-
Download the GPG key command line tools from here.
- pick the correct download for your operating system.
- For my Windows system, I picked the simple installer for GnuPG as I don’t need the full featured version for what I want to do.
-
Open Git Bash or whichever terminal you prefer using
-
Create your GPG key by entering the following command:
- If you have a version of git of 2.1.17 or later, enter
gpg --full-generate-key
- If you are not on version 2.1.17 or later, use
gpg --default-new-key-algo rsa4096 --gen-key
and skip to step 6.
- If you have a version of git of 2.1.17 or later, enter
-
Press Enter for the defult key type. We are making an RSA key for this tutorial
-
Enter
4096
for the key size and hit enter -
For the length of time, hit enter if you want to accept the default expiration date of never or select the time frame from the options shown.
-
Enter your full name that you want to be associated with your key.
-
Enter your commit email. If you have set your email to private on GitHub or you don’t want people to know your email, use the no reply email provided to you on GitHub under the email settings.
- You need to make sure that this email matches the email you set in your config file when you set up git.
-
You can add a comment to label what this key is for.
-
Create a password and confirm it.
-
Type
gpg --list-secret-keys --keyid-format=long
to list your gpg key. -
Copy the long form of the key from the sec: section of the output key. This is after the section that lists your key length (4096) and before the expiration date. In the below example this is
3AA5C34371567BD2
$ gpg --list-secret-keys --keyid-format=long
/Users/hubot/.gnupg/secring.gpg
------------------------------------
sec 4096R/3AA5C34371567BD2 2016-03-10 [expires: 2017-03-10]
uid Hubot
ssb 4096R/42B317FD4BA89E7A 2016-03-10
-
Type
gpg --armor --export 3AA5C34371567BD2
replacing3AA5C34371567BD2
with your key. -
Copy the output key from
-----BEGIN PGP PUBLIC KEY BLOCK-----
to-----END PGP PUBLIC KEY BLOCK-----
.
Add Your GPG Key to GitHub
We will add the GPG key in the same way as we added our SSH key.
-
Go to settings in GitHub and find the SSH and GPG keys option
-
Click on to “New GPG key”
-
Paste your public key into the new key field.
-
You may be prompted to reenter your GitHub password to save the GPG key.
Add your GPG key to Git
-
In the terminal copy your key’s long form again. For this example, I will use
3AA5C34371567BD2
. -
Enter the following replacing my key with yours:
git config --global user.signingkey 3AA5C34371567BD2
Signing commits
-
If you want git to sign all commits in a specific repsitory, enter
git config commit.gpgsign true
into Git Bash. If you only want to sign in all repositories on the computer, entergit config --global commit.gpgsign true
into Git Bash.- Note that this only works in git versions 2.0.0 and above.
-
If you want to sign a specific commit, add the
-S
flag to the call to commit.- ex:
git commit -S
- ex:
-
Once you commit changes, you will be prompted to input your password for your GPG key.
Signing Tags
-
Add a
-s
flag to the tag- ex:
git tag -s v1.0.0
- Note that this is a lower case s for tags and an uppercase S for commits.
- ex:
-
verify a tag’s signature using the
-v
flag.- ex:
git tag -v v1.0.0
- ex: