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.
Git uses GPG (GNU Privacy Guard) for signing commits. To start signing commits, install GPG:
sudo apt install gpg
brew install gnupg
Once GPG is installed, generate a key pair (public and private key) to sign commits.
gpg --full-generate-key
To sign commits, configure Git to use the generated GPG key.
gpg --list-secret-keys --keyid-format LONG
Set this key ID in Git:
git config --global user.signingkey
gpg --armor --export
2. Copy the key and add it to GitHub under Settings > SSH and GPG keys.
To sign a single commit, add the -S
flag to your commit command:
git commit -S -m "Your commit message"
-S
flag ensures the commit is signed with the GPG key.To check if a commit is signed and verify its signature:
git log --show-signature
commit abcdef12345
gpg: Signature made ... using RSA key ID 3AA5C34371567BD2
gpg: Good signature from "Your Name "
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.
For projects on GitHub, you can enable branch protection rules to require 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.
Though GPG is commonly used, you can also use SSH keys for commit signing, especially if you already use SSH for repository access.
git config --global gpg.format ssh
git config --global user.signingkey ~/.ssh/id_rsa.pub
If your GPG key has a passphrase, using the GPG agent can help avoid repeated passphrase prompts:
gpg --list-secret-keys --keyid-format LONG
export GPG_TTY=$(tty)
Adjust cache settings in .gnupg/gpg-agent.conf
for convenience.
git config --global user.signingkey
export GPG_TTY=$(tty)
gpg --list-secret-keys --keyid-format LONG
gpg-agent.conf
.GPG keys are essential for signing, so regularly back up your private key in a secure location.
A strong passphrase ensures that even if your key file is compromised, unauthorized use remains challenging.
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 !❤️