Skip to content

Instantly share code, notes, and snippets.

@luckygoswami
Created May 5, 2025 17:59
Show Gist options
  • Select an option

  • Save luckygoswami/3f7cd9a2a7787314d44290941f0665f3 to your computer and use it in GitHub Desktop.

Select an option

Save luckygoswami/3f7cd9a2a7787314d44290941f0665f3 to your computer and use it in GitHub Desktop.
Step-by-step guide to transfer your existing GPG key to a new machine for Git commit signing. Covers key export/import, Git configuration, and GitHub setup for seamless migration.

Title: πŸ”‘ How to Import and Configure an Existing GPG Key on a New System

Description:

Step-by-step guide to transfer and set up an old GPG key (for Git commit signing) on a new machine.


1. Export Keys from Old System

(Skip if you already have the key files)

Export Private Key:

gpg --export-secret-keys --armor YOUR_KEY_ID > private-key.asc
  • Replace YOUR_KEY_ID with your key’s fingerprint or email.

Export Public Key:

gpg --export --armor YOUR_KEY_ID > public-key.asc

Export Trust DB (Optional):

gpg --export-ownertrust > trustdb.txt

2. Transfer Files to New System

Copy these files to the new machine:

  • private-key.asc
  • public-key.asc (optional, if needed)
  • trustdb.txt (optional)

3. Import Keys on New System

Install GPG

  • Linux: sudo apt install gnupg
  • Mac: brew install gnupg
  • Windows: GPG4Win

Open Bash in the same directory as your keys

Import Private Key:

gpg --import private-key.asc

Import Public Key (if separate):

gpg --import public-key.asc

Import Trust DB (Optional):

gpg --import-ownertrust trustdb.txt

4. Verify Imported Keys

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

Check if your key appears.


5. Configure Git to Use GPG

Set Key for Git:

git config --global user.signingkey YOUR_KEY_ID

Enable Commit Signing:

git config --global commit.gpgsign true

Enable Tag Signing (Optional):

git config --global tag.gpgsign true

Set GPG Program Path on Windows (Optional):

git config --global gpg.program "C:\Program Files (x86)\GnuPG\bin\gpg.exe"

7. Add GPG Key to GitHub

(Skip if the key already exists in GitHub account)

  1. Get your public key:
    gpg --armor --export YOUR_KEY_ID
  2. Paste it at:
    GitHub β†’ Settings β†’ SSH and GPG Keys β†’ New GPG Key

βœ… Done! Your old GPG key is now active on the new system.

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