Git Signing Commits

In Git, signing commits adds a cryptographic layer that authenticates the author of each commit. This feature helps verify that each commit originates from a trusted individual and has not been tampered with. Git signing is particularly useful in collaborative and open-source projects where the authenticity of contributions is crucial. This chapter will explain the steps to sign commits, verify them, and integrate this practice into your workflow.

Why Sign Commits?

The Importance of Signed Commits

  • Signing commits provides security by ensuring that a commit is from the declared author and hasn’t been altered.
  • Projects that rely on trust and integrity, such as open-source and enterprise repositories, often use signed commits to authenticate contributions.

Benefits of Using Signed Commits

  • Enhanced Security: Protects against unauthorized or accidental changes.
  • Authentic Contributions: Allows verifiable proof of authorship for each commit.
  • Transparent History: A trusted commit history fosters confidence among project contributors.

Setting Up Git to Sign Commits

Choosing and Installing GPG

Git uses GPG (GNU Privacy Guard) for signing commits. To start signing commits, install GPG:

Linux

				
					sudo apt install gpg

				
			

macOS

				
					brew install gnupg

				
			

Windows: Download from GPG4Win.

Generating a GPG Key

Once GPG is installed, generate a key pair (public and private key) to sign commits.

				
					gpg --full-generate-key

				
			

Key Generation Steps:

    1. Select the type of key (typically RSA and RSA).
    2. Specify a key length (2048 or 4096 bits for stronger encryption).
    3. Set expiration (or leave it without expiration).
    4. Provide your name and email (use the same email as your Git user.email).
    5. Optionally, set a passphrase for additional security.

Configuring Git to Use Your GPG Ke

 To sign commits, configure Git to use the generated GPG key.

  • Identify your GPG key ID:
				
					gpg --list-secret-keys --keyid-format LONG

				
			
  • Look for the key ID under your email, usually a 16-character string.
  • Set this key ID in Git:

				
					git config --global user.signingkey <your-key-id>

				
			

Associating the GPG Key with Your GitHub Account

  1. Export your GPG public ke
				
					gpg --armor --export <your-key-id>

				
			

2. Copy the key and add it to GitHub under Settings > SSH and GPG keys.

Signing Commits in Git

Signing a Single Commit

To sign a single commit, add the -S flag to your commit command:

				
					git commit -S -m "Your commit message"

				
			

Explanation:

  • The -S flag ensures the commit is signed with the GPG key.
  • If your GPG key is protected by a passphrase, Git will prompt you to enter it.

Verifying a Signed Commit

To check if a commit is signed and verify its signature:

				
					git log --show-signature

				
			

Explanation:

  • This command shows each commit in the log with a verification status, confirming if the commit is correctly signed.

Example Output:

				
					commit abcdef12345
gpg: Signature made ... using RSA key ID 3AA5C34371567BD2
gpg: Good signature from "Your Name <youremail@example.com>"

				
			

Enforcing Signed Commits

Configuring Git to Sign All Commits by Default

To avoid having to add -S every time, configure Git to sign all commits by default:

				
					git config --global commit.gpgSign true

				
			

This configuration automatically signs each commit, providing consistency without requiring additional flags.

Requiring Signed Commits in GitHub

For projects on GitHub, you can enable branch protection rules to require signed commits:

  1. Go to Settings > Branches.
  2. Select Branch protection rules and enable Require signed commits.
  3. Save changes to enforce signed commits on protected branches.

Advanced Topics in Git Signing

Using Aliases for Signed Commits

To simplify signed commits, you can create a Git alias:

				
					git config --global alias.scommit 'commit -S'

				
			

Now, using git scommit signs commits without needing to type -S each time.

Using SSH Keys for Signing (Alternative Method)

Though GPG is commonly used, you can also use SSH keys for commit signing, especially if you already use SSH for repository access.

  1. Ensure SSH is configured for your GitHub account.
  2. Set Git to use SSH for signing
				
					git config --global gpg.format ssh
git config --global user.signingkey ~/.ssh/id_rsa.pub

				
			

Enabling GPG Agent for Passphrase Caching

If your GPG key has a passphrase, using the GPG agent can help avoid repeated passphrase prompts:

  1. To cache passphrases, configure the GPG agent with these commands:
				
					gpg --list-secret-keys --keyid-format LONG
export GPG_TTY=$(tty)

				
			

Adjust cache settings in .gnupg/gpg-agent.conf for convenience.

Troubleshooting Common Issues

Error: “GPG Failed to Sign the Data”

  • Solution: Make sure the correct key is associated by running
				
					git config --global user.signingkey <your-key-id>

				
			

If the error persists, restart the GPG agent or try:

 
				
					export GPG_TTY=$(tty)

				
			

Error: “No Secret Key”

  • Solution: Confirm that the key ID in Git configuration matches your GPG key ID by running
				
					gpg --list-secret-keys --keyid-format LONG

				
			

Passphrase Prompting for Each Commit

  • Solution: Enable GPG agent caching as described above, or consider setting a reasonable cache duration in gpg-agent.conf.

Best Practices for Signing Commits

Regularly Backup Your GPG Key

GPG keys are essential for signing, so regularly back up your private key in a secure location.

Protect Your Private Key with a Strong Passphrase

A strong passphrase ensures that even if your key file is compromised, unauthorized use remains challenging.

Review Signed Commits Before Merging

Especially in collaborative projects, verify commit signatures before merging them into protected branches. This helps prevent untrusted changes from entering the main codebase.

Signing Git commits provides a trusted way to ensure the authenticity and integrity of changes within a Git repository. By signing commits, developers can establish a secure and verifiable history, crucial for open-source projects and collaborative workflows. This chapter has walked through the process of setting up GPG, signing and verifying commits, and implementing best practices for secure development. Happy coding !❤️

Table of Contents

Contact here

Copyright © 2025 Diginode

Made with ❤️ in India