Skip to content

A project by

All articles →
Codebase, Git

Signing Git commits and tags with GPG

Thomas Höll Platform Architect

· 3 min read

Create a new PGP key

$ dnf install gnupg2

$ gpg --gen-key
# Enter your e-mail address and name:

Protect the key with a strong passphrase when prompted; a private key without a passphrase can be used by anyone who gets hold of the file.

Now, retrieve the public key id:

$ gpg --list-secret-keys --keyid-format LONG

/home/user/.gnupg/pubring.kbx
----------------------------
sec   ed25519/485CAB7FC28F950B 2022-07-13 [SC] [expires: 2024-07-12]
      47B43006BDDF5988443CDD7D485CAB7FC28F950B
uid                 [ultimate] Firstname Lastname <user@domain.tld>
ssb   cv25519/CED0142D1E47A615 2022-07-13 [E] [expires: 2024-07-12]

Note down the public key id (${GPG_PUBLIC_KEY_ID}). In the output above, 485CAB7FC28F950B is the key id.

Copy the public key to GitHub

Retrieve the full public key:

$ gpg --armor --export ${GPG_PUBLIC_KEY_ID}

Go to GitHub's GPG keys section and paste in the exported public key:

github-add-pgp-key.png

Create a backup of the newly created key

$ gpg -o private.gpg --export-options backup --export-secret-keys user@domain.tld

Store the generated private.gpg in your password manager and delete the file afterwards. Never commit it or leave it lying around unencrypted.

Test signing and maybe update .bashrc

Execute the signing command:

$ echo "test" | gpg --clearsign

If you receive the error

error: gpg: signing failed: Inappropriate ioctl for device 
fatal: gpg: [stdin]: clear-sign failed: Inappropriate ioctl for device

you have to update your .bashrc to set the environment variable GPG_TTY:

GPG_TTY=$(tty)
export GPG_TTY

Do not forget to reload the .bashrc.

Signed Git commits and tags

# For your global configuration, set the signing key:
$ git config --global user.signingkey ${GPG_PUBLIC_KEY_ID}

Signing single commits and tags

If you want to sign commits and tags by yourself, you can use

# To sign a single commit:
$ git commit -S -a -m "A signed commit"

# To sign a tag:
$ git tag -s v1.0.0-signed

Signing all commits and tags

Instead of manually signing tags and commits, you can enable the signing by default:

# Enable signing of all commits:
$ git config --global commit.gpgsign true
# Enable signing of all tags, since Git 2.23:
$ git config --global tag.gpgSign true

Publish the GPG key

$ gpg --keyserver hkps://keys.openpgp.org --send-keys ${GPG_PUBLIC_KEY_ID}

The old SKS keyserver pool (e.g. pgp.mit.edu) is no longer reliable. keys.openpgp.org only publishes your user id (name and e-mail address) after you have confirmed the verification e-mail it sends you.

Restore the GPG key

If you move your GPG key to another machine, you have to restore it there. Retrieve the private key from your password manager and execute:

$ gpg --import-options restore --import private.gpg

# Edit imported key:
$ gpg --edit-key user@domain.tld
# Enter `trust`:
gpg> trust
# Enter `5` to trust your key ultimately:
gpg> 5
# Confirm with `y`:
gpg> y
# Leave the key editor:
gpg> quit

Written by

Thomas Höll

Platform Architect

Profile on dreitier.com ↗