Add GPG key to Git
Prerequisites
First import a Keybase PGP key to GPG.
Instructions
- Open Terminal.
- If you have previously configured Git to use a different key format when signing with
--gpg-sign
, unset this configuration so the default format ofopenpgp
will be used.git config --global --unset gpg.format
- 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
NoteSome GPG installations on Linux may require you to use
gpg2 --list-keys --keyid-format LONG
to view a list of your existing keys instead. In this case you will also need to configure Git to usegpg2
by runninggit config --global gpg.program gpg2
. - 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 /Users/hubot/.gnupg/secring.gpg ------------------------------------ sec 4096R/3AA5C34371567BD2 2016-03-10 [expires: 2017-03-10] uid Hubot <hubot@example.com> ssb 4096R/4BB6D45482678BE3 2016-03-10
- 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
:
Alternatively, you may want to use a subkey. In this example, the GPG subkey ID isgit config --global user.signingkey 3AA5C34371567BD2
4BB6D45482678BE3
:
If you use multiple keys and subkeys, then you should append an exclamation markgit config --global user.signingkey 4BB6D45482678BE3
!
to the key to tell git that this is your preferred key. Sometimes you may need to escape the exclamation mark with a back slash:\!
. - Optionally, to configure Git to sign all commits and tags by default, enter the following command:
For more information, see Signing commits.git config --global commit.gpgsign true git config --global tag.gpgSign true
- To add your GPG key to your
.bashrc
startup file, run the following command:[ -f ~/.bashrc ] && echo -e '\nexport GPG_TTY=$(tty)' >> ~/.bashrc