In this article, we will learn how to sign git commits and tags to verify that the code actually came from you and that it wasn’t maliciously altered by an attacker while you were transferring it.
GnuPG is a complete and free implementation of the OpenPGP standard. GnuPG allows you to encrypt and sign your data and communications; it features a versatile key management system, along with access modules for all kinds of public key directories. GnuPG, also known as GPG, is a command line tool with features for easy integration with other applications.
It is a common practice in software development to sign software packages to prove their authenticity. In the same way, it is possible to sign Git commits and tags, usually with GPG, to prove that the code came from you and that it wasn’t maliciously altered by an attacker.
Checkout related posts:
- SSH – Generating and Working with ssh keys
- How to ssh through host(jumpserver) to reach another server
Ensure that you have GPG installed
GPG is a command line tool with features for easy integration with other applications. It is a free encryption software which can be used to encrypt and decrypt files. We will use it to sign our Git commits and tags. On a UNIX-like operating systems like Ubuntu and MacOS, gpg
usually comes pre-installed. For windows, you have to download and install GPG yourself.
Confirm the installation by checking the version using this command:
|
|
If gpg
doesn’t work for you, then try writing gpg2
instead and see if that works. If the gpg2
command works for you, you can tell Git to use it for signing commits with the following command:
|
|
As a matter of fact, Git doesn’t care what program you use for signing your commits as long as it works like GPG.
Configuring GPG keys
Once the gpg command is working as expected, we can generate the key pairs - similar to what we do with SSH keys.
The following command will guide you through the GPG key creation process.
|
|
For more options, you can safely use the following settings:
- Key type: RSA and RSA.
- Size of key: 4096 bits.
- Validity of key: zero (unless you want your key to expire).
|
|
Next, GPG will ask for the following details to establish your identity. Here’s an example of the information GPG will ask for:
|
|
If you are planning to use different email addresses on different projects, you’ll have to generate one GPG key for each email address.
GPG will also ask you to create a password for the key. You’ll be prompted for this password whenever you try to use your key. I’d recommend choosing a strong and memorable password.
Important: Make sure you keep your GPG keys safe, especially the private key.
In order to put your GPG keys to use, you will need access to your key ID and the public key. We’ll see how to get those in the sections below.
Listing GPG Keys
Once you have setup a number of GPG keys, you might want to see a list of all your keys. You can do that with the following command:
List all GPG public keys
|
|
List all GPG secret keys.
|
|
For our example in this tutorial, we’ll only see the keys for a particular identity. We do this by appending an email address to the end of one of the above commands:
|
|
If you run the command without the email parameter, you will see all your GPG keys.
Getting GPG key ID
To get the ID of your GPG key, use the command above to see a list of keys first. Now, focus on the line that says sec
, i.e. rsa4096 2022-05-17 [SC]
. The part after the below that is the GPG key ID. For example: 8527AA1318C42445DC08C06ED722AF52CA1CE58C
.
Getting GPG public key
To see the public key, you need to have a key ID as mentioned above. Use the following command to see your full public key:
The syntax is: gpg --armor --export KEY-ID
|
|
Deleting GPG Keys
If you ever want to remove a public or private key, you need to run one of the following commands depending on your needs:
Deletes GPG secret key.
|
|
Deletes GPG public key for an email
|
|
When deleting, start with the private key
Configuring Git
Now, we need to tell Git about our GPG keys to be able to sign and verify things.
Add GPG keys to Git repository manager
Most Git repository managers like GitHub, GitLab and BitBucket provide an option to add GPG public keys to your account. The option to add your GPG public key to your Git repo manager is usually under profile settings.
Run the command gpg --armor --export KEY-ID
to get your GPG public key and add it to your repository manager. These keys are then used to generate badges to indicate if your commits are verified. This lets your team members easily check if your commits are signed and hence, authentic.
Add GPG keys to Git command-line tool
Use the following command to tell your command-line tool to use a specific GPG key for signing your commits:
The syntax is git config user.signingkey KEY-ID
|
|
You can use git config --global user.signingkey KEY-ID
to save this in your global Git settings which will then be used for all projects.
|
|
Signing Commits
Now that the GPG keys are in place, it’s actually time to sign commits and tags!
Please note the key to use to sign. You can get by listing the key and grabbing ID, 10630F587CF32D001E3ED7591442DEFCB04678A3
in my case.
|
|
Signing Git commits
To create a signed commit, add the additional parameter -S
to your git commit
command like this:
|
|
Doing this will show you a dialog where you will have to enter your GPG password to sign and make the commit.
Signing Git tags
To create a signed tag, add the additional parameter -s
to your git tag
command like this:
|
|
Doing this will show you a dialog where you will have to enter your GPG password to sign and make the commit.
Always sign Git commits
If you’ve decided that you always want to sign your commits and tags, then you can update your git configuration accordingly with the following command:
Enable signing for the project.
|
|
Enable signing globally.
|
|
Conclusion
Signing GPG commits is an extra layer of security that help verify if a commit or a tag was actually made by you. This can be done by generating a key and configuring it with Git. Signed git commits usually have a “verified” badge on Git repository managers like GitHub, GitLab, BitBucket, etc.