Alvin Novian
Back to index

Setup GPG for Git

To start, what the hell is GPG? you can learn here. Ok, cool .. but why would you need it? the thing is, Git is a distributed version control, meaning everyone can alter the history of it and push to whatever remote URL they want to, so you can impersonate someone to commit something, this action is called as Commit Spoofing, you can learn detail here

So if you want to avoid it, you gotta make sure your commit can be verified through some kind of signing concept (hey that’s GPG!), thus GPG comes in handy. All Git hosting services (eg. GitHub, GitLab, BitBucket) should support GPG so you can integrate this for you repo

How it works

Okay enough talk, you’re in and how can can we use GPG for my Git workflow?

  1. You will need to generate GPG key on your machine that you will use to do git commit
  2. You need to make sure that key is signed for every commit that you want to verify
  3. You need to tell your GPG public key to the Git Hosting Service that you use, so they can verify it and know that it’s you who does all the commit

That’s simple, let’s see the step by step guide to setup GPG, I will use Linux Fedora as the machine that I will be using and I will use GitHub as an example

Step-by-step setup

Install GPG if you don’t have it yet (Linux Fedora have GPG by default) and then you can generate a new key by running:

gpg --full-generate-key

you’ll be asked for several options, generally I will use:

  • RSA and RSA as key algorithm
  • 4096 bits long key
  • and set the key to expires in 1y (years)
  • you’ll also be asked for your name and email address, make sure email address is the same as the one that you use in GitHub

After new key has been generated, you can check it by running:

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

you’ll see an some kind of output like this:

[keyboxd]
--------
sec   rsa4096/XXXXXXX 2023-12-06 [SC] [expires: 2024-12-05]
      YYYYYYYYYYYYYYYYYYYYYYYYYYYYYY
uid   [ultimate] Your Name <youremail@something.com>
ssb   rsa4096/ZZZZZZZ 2023-12-06 [E] [expires: 2024-12-05]

XXXXXXX is the ID and we will be using it to take out the public key, here’s how to take the public key:

gpg --armor --export XXXXXXX

Alright, now you need to add this public key into GitHub, so that it can verify that it’s you, you can refer to GitHub’s official doc to add GPG key there, here’s how

Cool, now your machine and GitHub share the same GPG key so you’re verifiable. Then we need to make sure that you signed every commit with the GPG key that you have. You can set Git config globally to always enable the signing process & to use the same GPG key, but I personally like to do it per-repo basis

Because I don’t need GPG in all of my Git directory, for example there are cases where I use Git but never publish it to a Hosting Provider, usually it’s when I research/experiment something, I will only use Git for tracking the progress locally .. but it’s up to you, really

So how to do it? In your Git directory, run this command:

git config --local commit.gpgsign true
git config --local user.signingkey XXXXXXX

Nice, now everytime you do git commit it will ask passphrase for that key, and later when you push it to GitHub, it will be shown as Verified

Verified commit history

You can even force GitHub to mark all commit without GPG key as Unverified and you can sit relax knowing your coworker or someone else won’t commit proofing something with your identity without you even knowing

What a nice life!