Signing git commits will help verify that they come from a trusted source. This prevents a commit author from pretending to be someone else.
$ gpg --list-secret-keys --keyid-format LONG
- If GPG keys do not exist, generate a new GPG key
- Else, add it to your GitHub account
- Generate GPG key pair.
- If gpg version is 2.1.17 or greater:
gpg --full-generate-key
- Else:
$ gpg --default-new-key-algo rsa4096 --gen-key
- Choose any kind of key you prefer, leave blank to use the default.
- If gpg version is 2.1.17 or greater:
- Enter
4096
or greater for desired key size. - Enter how long the key will be valid for. Leave blank for a non-expiring key.
- Verify if your choice is correct.
- Provide the necessary information for your User ID (UID).
- Make sure your UID email address is the same one used for your GitHub account.
- As a best practice, do not include a comment in your UID.
- Verify if your UID is correct.
- Enter a secure passphrase.
- List all your GPG keys and copy the GPG key ID you would like to use. In the example below, the ID 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
- Generate the GPG public key with:
$ gpg --armor --export <Insert GPG key ID>
- Copy the resulting GPG public key which starts with
-----BEGIN PGP PUBLIC KEY BLOCK-----
and ends with-----END PGP PUBLIC KEY BLOCK-----
, and add it to your GitHub account.
- Go to your profile settings.
- Select
SSH and GPG keys
- In the GPG keys section, click the
New GPG
button. - Paste your public GPG key and click
Add GPG key
- Tell Git about your signing key.
$ git config --global user.signingKey 3AA5C34371567BD2
- To sign commits, use:
$ git commit -S -m <commit message>
- To automatically sign commits without the
-S
flag, use:$ git config --global commit.gpgsign true