Skip to main content

Git Additional Configuration

Generating a GPG Key

gpg on Windows

For Windows users, use gpg4win.

  1. Generate a GPG key pair:
gpg --default-new-key-algo rsa4096 --gen-key
  1. Specify name and email.
  2. 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
  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-----.
Commiting

Use git commit -S ... to sign commits, or sign commits by default:

git config --global commit.gpgsign true
Using gpg on Windows

For using gpg4win:

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 GitHub or GitLab: Add the GPG key to your GitHub account.

Migrating GPG keys

  1. On your old machine, 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
  1. On the new machine, import those previously exported files:
gpg --import public.asc
gpg --import secret.asc

General Config

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