Signing Commits and Tags

Updated · 13 min read
hardGit 2.53gitsigningsshgpgsecurityverify-commitinterview

Signing a commit or tag means attaching a digital signature, made with a private key that only you have, to the commit or tag object. Anyone with your matching public key can then check two things: that the commit really came from the owner of that key, and that it hasn't been changed since. Git can sign with an SSH key (the simpler choice, available since Git 2.34) or with a GPG key. It is used to prove authorship on shared projects, and many companies and open source projects require signed commits on their main branches.

It is like the seal on a letter. Anyone can write "From: Navika" on an envelope. Only Navika has her seal, and a broken seal shows the letter was opened.

Why Signing Matters

Git records whatever name and email address it is given. Nothing stops anyone from committing as someone else:

Terminal
mkdir fee-tracker
cd fee-tracker
git init --quiet
echo 'Term 1 fee: $12,000' > fees.txt
git add fees.txt
git commit --quiet --author="Advitha Anamaneni <advitha@example.com>" -m "Lower the term fee"
git log --format="%an <%ae>: %s"
Output
Advitha Anamaneni <advitha@example.com>: Lower the term fee

Explanation: Navika made this commit, but it says Advitha wrote it. Hosting platforms would even show Advitha's profile picture next to it, because they match commits to accounts by email address. A signature can't be faked this way: it needs the private key.

A name can be typed; a signature must be made with the key

Syntax

Terminal
git commit -S -m "<message>"
git tag -s <name> -m "<message>"
git verify-commit <commit>
git verify-tag <tag>
git log --show-signature
Command or settingDescription
git config gpg.format sshIt is used to sign with an SSH key instead of GPG.
git config user.signingKey <key>It is used to choose the key: a path to an SSH public key, or a GPG key ID.
git commit -SIt is used to sign one commit.
git tag -s <name>It is used to create a signed tag.
git config commit.gpgSign trueIt is used to sign every commit automatically.
git config tag.gpgSign trueIt is used to sign every annotated tag automatically.
git config gpg.ssh.allowedSignersFile <file>It is used to tell Git which SSH keys belong to whom, so that it can verify signatures.
git verify-commit <commit> / git verify-tag <tag>It is used to check a signature.
git log --show-signatureIt is used to show the signature check of each commit in the log.

Example 1: Setting Up an SSH Signing Key

Navika makes a key just for signing. (You can also use your existing SSH key, such as ~/.ssh/id_ed25519.pub, described in "Adding an SSH Key".) Then she tells Git to sign with it:

Terminal
mkdir -p ~/.ssh
ssh-keygen -q -t ed25519 -N "" -C "navika@example.com" -f ~/.ssh/fee_signing
git config --global gpg.format ssh
git config --global user.signingKey ~/.ssh/fee_signing.pub
git config --global commit.gpgSign true
git config --global tag.gpgSign true
Output
 

Explanation: mkdir -p ~/.ssh makes sure the usual key folder exists. ssh-keygen created a private key ~/.ssh/fee_signing and a public key fee_signing.pub (-N "" means no passphrase, which keeps this example short; use a passphrase and an SSH agent for real keys). gpg.format ssh switches Git from GPG to SSH signing. user.signingKey names the public key; Git finds the private key next to it, or in your SSH agent. The last two settings sign every commit and annotated tag automatically, so -S and -s are no longer needed.

Example 2: A Signed Commit

Terminal
echo 'Term 1 fee: $12,000 (approved)' > fees.txt
git commit --quiet -am "Mark the term fee as approved"
git log --format="%G? %s"
Output
error: gpg.ssh.allowedSignersFile needs to be configured and exist for ssh signature verification
N Mark the term fee as approved
N Lower the term fee

Explanation: the commit was signed, but Git can't check it yet. %G? prints a letter for each commit's signature status, and both commits show N. For SSH signatures, Git needs a list of whose keys are whose, and without it, it reports the error and treats the signature as unknown. Keys are not tied to people automatically: someone has to say "this key belongs to navika@example.com".

Example 3: The Allowed Signers File

The allowed signers file lists, one per line, an email address and the public key allowed to sign for it. A team usually keeps one in the repository or on a shared drive:

Terminal
echo "navika@example.com namespaces=\"git\" $(cat ~/.ssh/fee_signing.pub)" > ~/.ssh/allowed_signers
git config --global gpg.ssh.allowedSignersFile ~/.ssh/allowed_signers
git log --format="%G? %GS %s"
Output
G navika@example.com Mark the term fee as approved
N  Lower the term fee

Explanation: the signed commit is now G (a good signature), and %GS shows who signed it. The first commit is N: no signature at all. namespaces="git" limits the key to Git signatures.

git verify-commit checks one commit. A key's fingerprint is different for every key, so the commands below replace it with <fingerprint> to keep this page's output the same on every run:

Terminal
git verify-commit HEAD 2>&1 | sed -E "s/SHA256:[A-Za-z0-9+\/]+/SHA256:<fingerprint>/"
git verify-commit HEAD~1; echo "exit code: $?"
Output
Good "git" signature for navika@example.com with ED25519 key SHA256:<fingerprint>
exit code: 1

Explanation: the signed commit has a good signature for navika@example.com. For the unsigned commit, git verify-commit prints nothing and exits with code 1, which makes it easy to use in scripts and CI checks.

Example 4: Signed Tags

Releases are a common place to require signatures, because users download exactly what a tag points to:

Terminal
git tag v1.0 -m "Fee Tracker 1.0"
git verify-tag v1.0 2>&1 | sed -E "s/SHA256:[A-Za-z0-9+\/]+/SHA256:<fingerprint>/"
Output
Good "git" signature for navika@example.com with ED25519 key SHA256:<fingerprint>

Explanation: because tag.gpgSign is on, git tag -m created a signed annotated tag. Without that setting, use git tag -s v1.0 -m "...". git tag -v v1.0 shows the tag's contents as well as the check.

Example 5: A Signature from an Unknown Key

Navika signs a commit with a different key that isn't in the allowed signers file:

Terminal
ssh-keygen -q -t ed25519 -N "" -f ~/.ssh/other_key
echo 'Late fee: $250' >> fees.txt
git commit --quiet -am "Add the late fee" --gpg-sign=$HOME/.ssh/other_key.pub
git log --format="%G? %GS %s"
Output
U  Add the late fee
G navika@example.com Mark the term fee as approved
N  Lower the term fee

Explanation: the new commit's signature is mathematically valid, but U means "good signature, unknown signer": the key isn't listed for anyone, so %GS is empty. git verify-commit would fail with "No principal matched." This is exactly what a forged commit signed with someone else's key looks like.

Signature Status Letters

%G?Meaning
GGood signature from a known signer
UGood signature, but the signer is unknown or not trusted
BBad signature: the commit was changed after signing
X / YGood signature, but the signature or the key has expired
RGood signature from a revoked key
EThe signature can't be checked, for example because the key is missing
NNo signature

Signing with GPG

GPG is the older way to sign, still widely used. It needs the gpg program and a GPG key. These commands are shown without output, because creating a GPG key is interactive:

Terminal
gpg --full-generate-key
gpg --list-secret-keys --keyid-format=long
git config --global gpg.format openpgp
git config --global user.signingKey 3AA5C34371567BD2
git commit -S -m "Add the hostel fee"

Explanation: --full-generate-key asks for the key type, size, expiry, your name and your email address (use the same one as user.email). --list-secret-keys --keyid-format=long shows the key's ID after sec ed25519/. Give that ID to user.signingKey. gpg.format openpgp is the default, so you only need it when switching back from SSH. GPG keys can expire and be revoked, which SSH keys can't, and verification uses your GPG keyring instead of an allowed signers file.

SSH signingGPG signing
Extra softwareNone (OpenSSH is everywhere)GnuPG
Key you probably already haveYes, for pushingUsually not
Expiry and revocationNoYes
Verification on your computerAllowed signers fileGPG keyring and trust levels
Supported by hosting platformsYesYes

"Verified" on Hosting Platforms

Hosting platforms check signatures against the keys their users uploaded, and show a Verified badge next to signed commits. Upload your public signing key to your account:

text
Settings, then SSH and GPG keys, then New SSH key. Choose "Signing Key" as the key type and paste
fee_signing.pub. (For GPG, choose New GPG key and paste the output of gpg --armor --export <key ID>.)
Branch protection rules and rulesets can then require signed commits.

The email address in each commit must match a verified email address on your account, or the commit shows as "Unverified".

Hashes and Integrity

A signature covers the whole commit object: the tree ID, the parent IDs, the author, the committer and the message. Because each ID is a hash of the object's contents, the tree ID pins every file and the parent ID pins all earlier history (see "Git Internals Overview"). So one signed commit vouches for the exact state of the whole project at that point.

That chain is only as strong as the hash function. Git has used SHA-1, and in 2017 researchers showed that two different files with the same SHA-1 hash can be made (the SHAttered attack). Since version 2.13, Git uses a hardened SHA-1 that detects files built for this kind of attack and refuses them, so the known attack doesn't work against Git. Git also supports repositories that use SHA-256 for every ID (git init --object-format=sha256), and Git 3.0 is planned to make SHA-256 the default for new repositories (see "Git 3.0: What Is Planned").

Common Mistakes

Signing without uploading the key. The platform shows "Unverified". Upload the public key as a signing key.

An email address that doesn't match. user.email must be a verified address on your hosting account.

No allowed signers file. Local verification of SSH signatures shows N or an error. Set gpg.ssh.allowedSignersFile.

Thinking a signature means the code was reviewed. It only proves who committed it.

Sharing or losing the private key. Anyone with it can sign as you. Protect it with a passphrase, and replace it (and update the platforms) if it leaks.

Under the Hood

When Git signs a commit, it builds the commit object as usual, passes its text to ssh-keygen -Y sign (or gpg), and inserts the resulting signature into the object as a gpgsig header, before the message. Because the signature is part of the object, it changes the commit's ID. To verify, Git removes the gpgsig header, passes the remaining text and the signature to ssh-keygen -Y verify together with the allowed signers file, and reads the result. A signed tag works the same way, except the signature is appended to the end of the tag's message. You can see the raw signature with git cat-file -p HEAD.

Interview Corner

Why sign Git commits?

Git records any author name it is given, so anyone can commit as someone else. A signature, made with a private key, proves the commit came from that key's owner and wasn't changed afterwards.

How do you sign commits with an SSH key?

git config --global gpg.format ssh, git config --global user.signingKey ~/.ssh/id_ed25519.pub, then git commit -S (or set commit.gpgSign true). Upload the key as a signing key on your hosting platform.

How do you verify a signed commit?

git verify-commit <commit> or git log --show-signature. For SSH signatures, configure gpg.ssh.allowedSignersFile with the keys you trust.

Is SHA-1 in Git broken?

Collisions were shown in 2017, but Git uses a hardened SHA-1 that detects those attacks. Git also supports SHA-256 repositories, which Git 3.0 plans to make the default.

Practice

Challenge: a signed release tag. Create a signed tag v1.1 on the latest commit with the message "Fee Tracker 1.1", then print the subject and %G? letter of the last three commits.

What does Git print? Before you open the solution, decide which letter each commit shows.

Show solution
Terminal
git tag v1.1 -m "Fee Tracker 1.1"
git verify-tag v1.1 > /dev/null 2>&1 && echo "v1.1 has a good signature"
git log -3 --format="%G? %s"
Output
v1.1 has a good signature
U Add the late fee
G Mark the term fee as approved
N Lower the term fee

The tag is signed (tag.gpgSign is on) with Navika's main key, so it verifies. The commits show U for the commit signed with the unknown key, G for the approved fee, and N for the first, unsigned commit.

Checkpoint. Set up SSH signing in your own Git config, upload the key to your hosting platform as a signing key, push a signed commit, and check that it shows Verified. Then run git log --show-signature -1 locally.

Quick check: signing commits and tags

4 questions
1What does a valid signature on a commit prove?
2Which setting makes Git sign with an SSH key?
3git log --format="%G?" prints U. What does it mean?
4Which file tells Git which SSH key belongs to which email address?

Key Takeaways

Quick reference

CommandIt is used to
git config --global gpg.format sshSign with SSH keys
git config --global user.signingKey <key>Choose the signing key
git config --global commit.gpgSign trueSign every commit
git commit -S / git tag -sSign one commit or tag
git verify-commit / git verify-tagCheck a signature
git log --format="%G? %GS %s"Show signature status in the log

Was this tutorial helpful?

Lesson 91 of 106