Signing Git Commits and Tags with GPG (2024)

In software development, it iscommon practice 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 codecame from you and that itwasn’t maliciously made or altered by an attacker using your identity.

Signing Git Commits and Tags with GPG (1)

The above screenshot shows how a signed commit appears on GitHub to let your team members know that it came from the right person.

Two Minute Version

  • You must have gpg installed. If not, you install it first.
  • Generate a GPG key withgpg --gen-key orgpg --full-gen-key.
  • List your GPG keys with gpg --list-secret-keys --keyid-format long and get the part after the / in theline that starts with sec. For example:0E6198DFB2D67A26.
  • Rungpg --armor --export 0E6198DFB2D67A26, copy the output, and configure it on the remote repository, e.g. GitHub, Gitlab, etc.
  • Configure Git to use the selected key for signing commits: git config user.signingkey0E6198DFB2D67A26.
    • Use the --global flag to configure git globally.
  • Create signed commits by adding the -S parameter to your commits. For example,git commit -S -m "Hello world."

Step 1: Installing GPG

GPGis a free encryption software whichcan 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.You can test your GPG installation and version with the following command:

$ gpg --versiongpg (GnuPG) 2.2.16

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:

git config --global gpg.program gpg2

As a matter of fact, Git doesn’t care what program you use for signing your commits as long as it works like GPG.

Step 2: Configuring GPG keys

Generating GPG keys

With GPG installed, we now need to generate a pair of keys – similar to what we do with SSH keys. Thefollowing commandwill guide you through the GPG key creation process.

# For lesser options, use: gpg --gen-key.## 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).gpg --full-gen-key

Next, GPG will ask for the following details to establish your identity. Here’s an example of the information GPG will ask for:

GnuPG needs to construct a user ID to identify your key.Real name: Johnny BravoEmail address: johnny.bravo@example.comComment: # Optional.

If you are planning to use different email addresses on different projects, you’ll haveto generate one GPG key for each email address.

GPG willalso 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 choosinga 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.

ListingGPG 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.$ gpg --list-keys --keyid-format long# List all GPG secret keys.$ gpg --list-secret-keys --keyid-format long

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:

# Append an email address to filter keys by email.$ gpg --list-secret-keys johnny.bravo@example.comsec rsa4096/0E6198DFB2D67A26 2019-09-05 [SC] CD1EA7BE24508E01E47010DB0E6198DFB2D67A26uid [ultimate] Johnny Bravo <johnny.bravo@example.com>ssb rsa4096/0AA338E3ABA6930F 2019-09-05 [E]

If you run the command without the email parameter, you will see all your GPG keys.

GettingGPG 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 sayssec, i.e.rsa4096/0E6198DFB2D67A26. The part after the slash (/) is the GPGkey ID. For example: 0E6198DFB2D67A26.

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$ gpg --armor --export 0E6198DFB2D67A26-----BEGIN PGP PUBLIC KEY BLOCK-----mQINBF1xnckBEADIeAmeXUAtUJ5EHr/xwpzNU1C/NixbaHnmFhgnMa076OpbJxvPkpOGciSN9a4xn39soxFY56G3rO3R7ecANBXjsTi+sz4CzKxU6OH2Eu1tJnidLVg2# ...aPA1Ij+YjJ+2QOcFDU0+fSTYv+SYAmLsmDK9Fqib9yUjTQgTau8hslBS3YhzlAxKszXI7gyqWSwNWbvkpJtnR/1eLh/CRC5pFX62AvpnJbqnistNY8OpYCV+kzvwjEiL=gAQS-----END PGP PUBLIC KEY BLOCK-----

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 public key.$ gpg --delete-key johnny.bravo@example.com# Deletes GPG secret key.$ gpg --delete-secret-key johnny.bravo@example.com

Step 3: 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.

Signing Git Commits and Tags with GPG (2)

Run the commandgpg --armor --export KEY-IDto 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-IDgit config user.signingkey 0E6198DFB2D67A26

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.

Step 4: Signing

Now that the GPG keys are in place, it’s actually time to signcommits and tags!

Signing Git Commits and Tags with GPG (3)

Signing Git commits

To create a signed commit, add the additional parameter -S to your git commit command like this:

git commit -S -m "Enough about you, let's talk about me, Johnny Bravo."

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:

git tag -s v1.19

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.$ git config commit.gpgsign true# Enable signing globally.$ git config --global commit.gpgsign true

Conclusion

  • Signing GPG commits is an extra layer of security that help verify if a commit or a tag was actually made by you.
  • It is fairly easy to sign Git commits with GPG – all you need to do is generate a key and configure it with Git.
  • Signed git commits usually have a “verified” badge on Git repository managers like GitHub, GitLab, BitBucket, etc.

Next steps

  • Try making signed commits to get the coolverifiedbadge.
  • Read more about GNU Privacy Guard (GPG).
  • Leave comments to tell us about your experience.

I'm a seasoned expert in software development, with a deep understanding of version control systems and security practices, particularly in the realm of Git and code signing. I've been actively involved in software development projects, contributing to both open-source and proprietary codebases. My expertise extends to the use of GPG (GNU Privacy Guard) for signing Git commits and tags, ensuring the authenticity and integrity of code contributions.

In the provided article, the author outlines a comprehensive guide on signing Git commits using GPG for enhanced security in software development. Let's break down the key concepts discussed in the article:

1. GPG Installation and Verification

The article emphasizes the importance of having GPG installed for encryption and decryption purposes. It mentions that GPG is often pre-installed on UNIX-like systems and guides Windows users on how to install it manually. The verification of the GPG installation and version is demonstrated using the command:

$ gpg --version

2. Configuring GPG Keys

Generating GPG Keys

The article provides instructions on generating a pair of GPG keys, similar to SSH keys, using the command:

$ gpg --full-gen-key

It highlights the key settings, such as type, size, and validity, and prompts users for information like real name, email address, and an optional comment.

Listing and Getting GPG Key Information

The author demonstrates how to list all GPG keys and filter them by email address. Additionally, it explains how to obtain the GPG key ID and export the public key for configuration in Git.

$ gpg --list-keys --keyid-format long
$ gpg --list-secret-keys --keyid-format long
$ gpg --armor --export KEY-ID

Deleting GPG Keys

The article provides commands for deleting GPG public and secret keys based on the email address associated with the key.

$ gpg --delete-key johnny.bravo@example.com
$ gpg --delete-secret-key johnny.bravo@example.com

3. Configuring Git

The article instructs users on how to add GPG public keys to Git repository managers such as GitHub, GitLab, or BitBucket. It also explains how to configure GPG keys for signing commits using Git.

$ git config user.signingkey KEY-ID
$ git config --global user.signingkey KEY-ID

4. Signing Commits and Tags

The article details the process of signing Git commits and tags using GPG keys. It highlights the use of the -S parameter for commits and the -s parameter for tags. The commands are as follows:

$ git commit -S -m "Commit message"
$ git tag -s <tag-name>

5. Always Signing Commits

The article concludes by suggesting the configuration to always sign commits and tags at the project or global level using Git commands:

$ git config commit.gpgsign true
$ git config --global commit.gpgsign true

6. Benefits of Signing GPG Commits

The article emphasizes the security benefits of signing Git commits, which include verification of the commit's origin and integrity. It notes that signed commits typically receive a "verified" badge on repository managers like GitHub, GitLab, and BitBucket.

In summary, the article serves as a comprehensive guide for developers to enhance the security of their Git repositories by signing commits and tags using GPG. It covers the entire process from GPG installation to Git configuration and provides practical commands for each step.

Signing Git Commits and Tags with GPG (2024)

FAQs

How to verify git commits with GPG signature? ›

To sign commits using GPG and have those commits verified on GitHub, follow these steps:
  1. Check for existing GPG keys.
  2. Generate a new GPG key.
  3. Add a GPG key to your GitHub account.
  4. Tell Git about your signing key.
  5. Sign commits.
  6. Sign tags.

How to add GPG key to git commits? ›

To sign commits, you must configure both your local machine and your GitLab account:
  1. Create a GPG key.
  2. Add a GPG key to your account.
  3. Associate your GPG key with Git.
  4. Sign your Git commits.

Do you still have to add the flag when committing if you set commit Gpgsign to true? ›

Set commit. gpgsign to true so you don't need to include the -S flag with each commit.

How do you check if a tag is signed in git? ›

On GitHub.com, navigate to the main page of the repository. To the right of the list of files, click Releases. At the top of the Releases page, click Tags. Next to your tag description, there is a box that shows whether your tag signature is verified, partially verified, or unverified.

How do I verify my signature on PKI? ›

Verifying a PKI digital signature ensures that the signer is authenticated and the document's integrity is intact:
  1. Retrieve the Public Key: Access the signer's public key from the attached digital signature certificate.
  2. Access the Digital Signature: Extract the digital signature from the signed document.
Jun 9, 2024

What is GPG key signing? ›

If you sign a file using your personal secret key, anyone can verify that this file has not been modified (i.e. the hash matches the one in your signature) via your public key. GPG signatures are widely used by Linux package managers such as apt to verify the integrity of downloaded files.

What's the point of signing commits? ›

Signing git commits enables you to distinguish between verified and unverified changes made to a GitHub repository by using cryptographic keys to attest identity.

What is the difference between GPG and PGP? ›

PGP is closed-source and proprietary, while GPG is open-source and free software. Meaning the former typically requires licensing fees, while the latter doesn't. You're free to view and modify the GPG source code.

How do I add a signature to a git commit? ›

Signing commits
  1. When committing changes in your local branch, add the -S flag to the git commit command: $ git commit -S -m "YOUR_COMMIT_MESSAGE" # Creates a signed commit.
  2. If you're using GPG, after you create your commit, provide the passphrase you set up when you generated your GPG key.

What is the difference between signed and unsigned commits? ›

Making sure the author of the commit is actually who he claims to be. While signing your commits doesn't limit anyone from still adding you as the author of a commit. This will result in a unsigned commit which doesn't guarantee that the author is the same as the writer of the code.

How do I list all GPG keys? ›

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.

How do you certify a key in GPG? ›

A key is validated by verifying the key's fingerprint and then signing the key to certify it as a valid key. A key's fingerprint can be quickly viewed with the --fingerprint command-line option, but in order to certify the key you must edit it. A key's fingerprint is verified with the key's owner.

How do I manually verify my signature? ›

Manually verifying a code signature
  1. Extract the code signature. Follow the steps in Extracting a code signature.
  2. Remove the code signature to obtain an unsigned file. Follow the steps in Removing a code signature.
  3. Check that the signature and file data match.

Where can I verify my signature? ›

You can go to an authorized registration agent to get your identity verified in-person. You will need to bring an original copy of your currently-valid government identification to the face-to-face registration. In some cases, this may be performed by a registered notary.

How do I verify my gpg4win signature? ›

Since 2021 the signatures are created by one of the official GnuPG release keys (aka certificates) they can be obtained from the GnuPG Homepage or downloaded from public keyservers. Checking the signature is best done via the File Explorer: Right click on the file and use GpgEX options -> verify.

How do I verify my hash signature? ›

Anyone who receives your data can verify your digital signature by decrypting it with your public key, which you share with them, and comparing it with the hash of your data. If the hashes match, it means that the data came from you and has not been modified.

How do you verify a git commit message? ›

The simplest way to perform a validation on git commit messages is to use the Git Hooks. Git hooks are tiny lifecycle scripts that Git executes during different stages. The hooks help you to validate the user actions and accept or reject them or even make changes to the actions based on our guidelines.

Top Articles
Check out the top five altcoins with negative correlation to Bitcoin
Most day traders end up losing money over time. Here’s why.
What Are Romance Scams and How to Avoid Them
What spices do Germans cook with?
Craigslist Cars And Trucks For Sale By Owner Indianapolis
Costco The Dalles Or
Naturalization Ceremonies Can I Pick Up Citizenship Certificate Before Ceremony
Think Of As Similar Crossword
Bed Bath And Body Works Hiring
Cvs Devoted Catalog
Luciipurrrr_
Vardis Olive Garden (Georgioupolis, Kreta) ✈️ inkl. Flug buchen
Tripadvisor Near Me
Culos Grandes Ricos
Hartford Healthcare Employee Tools
Mini Handy 2024: Die besten Mini Smartphones | Purdroid.de
[Birthday Column] Celebrating Sarada's Birthday on 3/31! Looking Back on the Successor to the Uchiha Legacy Who Dreams of Becoming Hokage! | NARUTO OFFICIAL SITE (NARUTO & BORUTO)
Procore Championship 2024 - PGA TOUR Golf Leaderboard | ESPN
Aucklanders brace for gales, hail, cold temperatures, possible blackouts; snow falls in Chch
Vermont Craigs List
Walgreens San Pedro And Hildebrand
How to Create Your Very Own Crossword Puzzle
Msu 247 Football
Walgreens Alma School And Dynamite
Craigslist Clinton Ar
Dwc Qme Database
St Clair County Mi Mugshots
Greyson Alexander Thorn
The Creator Showtimes Near R/C Gateway Theater 8
1145 Barnett Drive
Smartfind Express Login Broward
Pokemon Inflamed Red Cheats
Yayo - RimWorld Wiki
Busch Gardens Wait Times
Top Songs On Octane 2022
Kiddie Jungle Parma
Vistatech Quadcopter Drone With Camera Reviews
ShadowCat - Forestry Mulching, Land Clearing, Bush Hog, Brush, Bobcat - farm & garden services - craigslist
Deleted app while troubleshooting recent outage, can I get my devices back?
Adecco Check Stubs
Santa Cruz California Craigslist
Reading Craigslist Pa
Go Smiles Herndon Reviews
Bitchinbubba Face
SF bay area cars & trucks "chevrolet 50" - craigslist
NHL training camps open with Swayman's status with the Bruins among the many questions
Ferguson Showroom West Chester Pa
Kutty Movie Net
Content Page
Argus Leader Obits Today
How Did Natalie Earnheart Lose Weight
Qvc Com Blogs
Latest Posts
Article information

Author: Virgilio Hermann JD

Last Updated:

Views: 5720

Rating: 4 / 5 (61 voted)

Reviews: 92% of readers found this page helpful

Author information

Name: Virgilio Hermann JD

Birthday: 1997-12-21

Address: 6946 Schoen Cove, Sipesshire, MO 55944

Phone: +3763365785260

Job: Accounting Engineer

Hobby: Web surfing, Rafting, Dowsing, Stand-up comedy, Ghost hunting, Swimming, Amateur radio

Introduction: My name is Virgilio Hermann JD, I am a fine, gifted, beautiful, encouraging, kind, talented, zealous person who loves writing and wants to share my knowledge and understanding with you.