Skip to main content

Git Additional Configuration

Generating a GPG key

  1. Generate a GPG key pair.
gpg --default-new-key-algo rsa4096 --gen-key
  1. Specify your name and email.
  2. 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
  1. 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
  1. Paste the text below, substituting in the GPG key ID you'd like to use. In this example, the GPG key ID is 3AA5C34371567BD2.
gpg --armor --export 3AA5C34371567BD2
  1. Copy your GPG key, beginning with -----BEGIN PGP PUBLIC KEY BLOCK----- and ending with -----END PGP PUBLIC KEY BLOCK-----.
gpg on Windows through gpg4win

Specify the path of gpg4win by using this command.

git config --global gpg.program "c:/Program Files (x86)/GnuPG/bin/gpg.exe"
gpg on Linux

Run this on your linux machine:

echo "export GPG_TTY=\$(tty)" >> ~/.profile
echo "export GPG_TTY=\$(tty)" >> ~/.bash_profile
echo "export GPG_TTY=\$(tty)" >> ~/.zshrc
export GPG_TTY=$(tty)

Additionally, you may want to add the GPG key to your GitHub or GitLab account. Learn more about adding the GPG key to your GitHub account.

Migrating GPG keys

On your old computer, export your current gpg key.

gpg --export -a $KEY > public.asc
gpg --export-secret-keys -a $KEY > secret.asc
getting key to export

Replace $KEY with your desired key to export.

Get current keys with:

gpg --list-secret-keys --keyid-format=long

Then, import those previously exported files to your new computer.

gpg --import public.asc
gpg --import secret.asc

General Git configuration

First-time use configuration for git.

git config --global user.name "YOUR_NAME"
git config --global user.email "YOUR_EMAIL"

git config --global credential.helper store
git config --global core.fileMode false
git config --global core.editor "nano"

git config --global commit.gpgsign true