How to Verify Git Commits with GPG on macOS

How to Verify Git Commits with GPG on macOS
Learn how to verify Git commits with GPG on macOS. This step-by-step guide covers setting up GPG, configuring Git, and ensuring the authenticity and integrity of your code commits for enhanced security and trust.

Share This Post

Are you looking to make your code more secure? Verifying Git commits with GPG on macOS can help. This process makes sure your code is authentic and trustworthy.

Ensuring the integrity and authenticity of your code is crucial in software development. One effective method is signing your Git commits with GPG (GNU Privacy Guard).

This guide will show you how to set up GPG key verification for your Git commits. Follow along to protect your code and improve your development workflow with verified commits.

What is GPG

GPG, or GNU Privacy Guard, is an encryption software that provides cryptographic privacy and authentication for data communication. It is used for secure data transmission, storage, and verification of data authenticity and digital signatures. GPG is a free software replacement for the PGP (Pretty Good Privacy) suite of cryptographic software.

Importance of GPG

  1. Data Security: GPG encrypts data, making it unreadable to unauthorized users.
  2. Authentication: It verifies the sender’s identity, ensuring the data has not been tampered with.
  3. Integrity: GPG provides mechanisms to check the integrity of the data, confirming it has not been altered.
  4. Confidentiality: Ensures that only the intended recipient can read the encrypted data.
  5. Non-Repudiation: The sender cannot deny having sent the message, as the digital signature is unique to them.

Verify Git Commits with GPG on Mac

Here is the process of setting up and using GPG to sign your Git commits on macOS, ensuring they are verified on platforms like GitHub.

Verify Your Email Address on GitHub

  1. Check Email Settings on GitHub:
  • Go to your GitHub email settings.
  • Ensure the email address you use in your Git commits is listed and verified.

Ensure Git is Using the Correct Email

  1. Configure Git with the Correct Email: Make sure Git is configured to use the correct name and email address associated with your GitHub account.
git config — global user.name “Your Name”
git config — global user.email “your-email@example.com”

2. Verify the Email in Your Commits: Check your recent commits to ensure it match the email verified on GitHub.

git log -1 --pretty=format:'%ae'

This command shows the email address of the latest commit.

Verify Git Commits with GPG on Mac

Here is the process of setting up and using GPG to sign your Git commits on macOS, ensuring they are verified on platforms like GitHub.

Generating a GPG Key

  1. Generate a GPG Key: Open your terminal and run:
gpg — full-generate-key

2. List Your GPG Keys:

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

3. Note Your Key ID: Find your key ID, typically a long string of characters.

Configuring Git to Use Your GPG Key

  1. Set GPG Key for Git:
git config — global user.signingkey YOUR_KEY_ID
git config — global commit.gpgsign true

2. Add Your GPG Key to GitHub: Export your GPG public key:

gpg — armor — export YOUR_KEY_ID
  • Copy the output.
  • Go to GitHub GPG keys settings.
  • Click the New GPG key.
  • Paste your GPG public key and save.

Bonus:

Installing and Configuring Pinentry-Mac
  1. Install Pinentry-Mac:
brew install pinentry-mac

2. Configure GPG to Use Pinentry-Mac: Open or create the GPG configuration file:

nano ~/.gnupg/gpg-agent.conf

3. Add the following line:

pinentry-program /opt/homebrew/bin/pinentry-mac

4. Restart the GPG Agent:

gpgconf — kill gpg-agent

5. Verify Pinentry-Mac Installation:

pinentry-mac

Amend Commits with the Correct Email

If your commits were made with the wrong email address, you must amend them to use the correct email.

  1. Start an Interactive Rebase: Start an interactive rebase to amend recent commits. Adjust HEAD~n where n is the number of commits you want to amend.
git rebase -i HEAD~n

2. Edit Each Commit: In the interactive rebase editor, change pick to edit For each commits, you want to amend.

edit commit1
edit commit2
To edit each commit Using Vim (Default on macOS)
  1. Navigate to the pick Commands: Use the arrow keys to move the cursor to the pick command that you want to change to edit.
  2. Enter Insert Mode: Press i to enter insert mode. You should see -- INSERT -- at the bottom of the screen.
  3. Edit the Command:
  • Use the arrow keys to move to the pick command.
  • Delete the pick command by placing the cursor over each character and pressing x.
  • Type edit in place of pick.

4. Repeat for Each Commit: Move to the next pick command and repeat the process to change it to edit.

5. Exit Insert Mode: Press Esc to exit insert mode.

6. Save and Exit Vim: Type :wq and press Enter to save the changes and exit Vim.

3. Amend Commit:

git commit — amend — author=”Your Name <your.email@example.com>” — no-edit -S

4. Continue Rebase (if needed):

git rebase — continue

5. Repeat for Each Commit: Repeat the amend and continue the process for each commit you want to correct.

Force Push to Remote Repository

After amending and signing all necessary commits:

git push — force

Verifying Commits on GitHub

  1. Check Commits: Navigate to your repository on GitHub and view your commits.
  2. Verified Badge: Ensure a green “Verified” badge is next to the commit messages.

Troubleshooting Common Issues

When setting up and verifying Git commits with GPG on macOS, users may encounter some common issues. Here, we address these issues and provide troubleshooting tips and solutions to ensure a smooth process.

Issue 1: GPG Software Installation

If you face difficulties during the GPG software installation, follow the correct installation process for macOS. Try the following troubleshooting steps:

  1. Double-check that you have downloaded the appropriate version of GPG software for macOS.
  2. Ensure that you have the necessary permissions to install software on your system.
  3. Try installing the software using different installation methods or from alternative sources.
  4. Consult online forums and communities to see if others have experienced similar installation issues and have found solutions.

Issue 2: GPG Key Pair Generation

If you encounter problems while generating your GPG key pair, consider the following solutions:

  1. Verify that you have entered the correct command to generate the key pair.
  2. Ensure you provide all the required information, such as your email address and name.
  3. Check if you have previously generated a key pair and may need to revoke it before generating a new one.
  4. Ensure your system has sufficient entropy, as a lack of entropy can cause key generation issues.
  5. Consult the documentation or seek assistance from the GPG software community if you are uncertain about any step of the key pair generation process.

Issue 3: Configuring Git with GPG

If configuring Git with GPG poses challenges, try the following troubleshooting steps:

  1. Confirm that you have entered the correct Git commands to configure GPG verification.
  2. Check if you have correctly configured the GPG executable path in the Git configuration.
  3. Verify that the GPG key ID you provided matches the key you generated.
  4. Ensure you execute the Git configuration commands in the correct directories or repositories.
  5. Seek guidance from online resources or the Git community to resolve configuration issues.

Conclusion

Following these steps, you can sign your Git commits with GPG on macOS, ensuring they are verified on GitHub. This process enhances the security and integrity of your code contributions.

For more detailed information, visit the GitHub documentation on signing commits.

Subscribe To Our Newsletter

Get updates and learn from the best

Related Articles

Lady Prowess

Learn how we helped 100 top brands gain success.

Let's have a chat