Generate and Prepare a new GPG key for Github

Generating a GPG key

gpg --full-generate-key
gpg --list-secret-keys --keyid-format=long

From the list of GPG keys, copy the long form of the GPG key ID you’d like to use. In this example, the GPG key ID is 3AA5C34371567BD2:

$ gpg --list-secret-keys --keyid-format=long
sec   4096R/3AA5C34371567BD2 2016-03-10 [expires: 2017-03-10]
uid                          Hubot 
ssb   4096R/42B317FD4BA89E7A 2016-03-10
gpg --armor --export 3AA5C34371567BD2
# Prints the GPG key ID, in ASCII armor format

Copy your GPG key, beginning with -----BEGIN PGP PUBLIC KEY BLOCK----- and ending with -----END PGP PUBLIC KEY BLOCK-----.


Export and Import GPG private key

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

Example output:

pub   4096R/ABC12345 2020-01-01 [expires: 2025-12-31]
uid                  Your Name <[email protected]>
sub   4096R/DEF67890 2020-01-01 [expires: 2025-12-31]

Remember the ID of your key (second column, after the slash, e.g. “ABC12345”). If you have a “sub” entry, you can ignore it.

Run this command to export your key:

gpg --export-secret-keys YOUR_ID_HERE > private.key

Run this command to import your key:

gpg --import private.key