Skip to content

Instantly share code, notes, and snippets.

@manthri-mohan-sai
Created January 10, 2023 03:01
Show Gist options
  • Save manthri-mohan-sai/59540d7fd6b78ed9fa8f1a1245dbb72d to your computer and use it in GitHub Desktop.
Save manthri-mohan-sai/59540d7fd6b78ed9fa8f1a1245dbb72d to your computer and use it in GitHub Desktop.
gpg setup

Import GPG keys

gpg --import <path to your private gpg.key>

Generate GPG keys

gpg --full-generate-key 

The recommended choices are:

  • Type of the key: RSA
  • Key size: at least 4096 bits
  • Key validity period: 1 year (it's a good practice to rotate the key once a year)

It is recommended to use the same username and e-mail address that is shown as the author of your commits. Specify the GitHub noreply email address if you plan to use the signature along with the email address privacy features.

List GPG Keys

gpg --list-keys

Use the gpg --list-secret-keys --keyid-format=long command to list the long form of the GPG keys for which you have both a public and private key. A private key is required for signing commits or tags.

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:

/Users/hubot/.gnupg/secring.gpg
------------------------------------
sec   4096R/3AA5C34371567BD2 2016-03-10 [expires: 2017-03-10]
uid                          Hubot <[email protected]>
ssb   4096R/4BB6D45482678BE3 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-----.

Signing GPG Keys

To set your primary GPG signing key in Git, paste the text below, substituting in the GPG primary key ID you'd like to use. In this example, the GPG key ID is 3AA5C34371567BD2:

git config --global user.signingkey 3AA5C34371567BD2

# Optionally, to configure Git to sign all commits by default, enter the following command:
git config --global commit.gpgsign true

Debug & Solutions

gpg: failed to commit

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment