Skip to main content

13 - Signing Git Commits with an SSH Key, and Really Verifying Them

An SSH signature can be correct and still untrusted: set up signed commits, read the G, U and N letters and write the allowed_signers file.

What do you think about this article?

13 - Signing Git Commits with an SSH Key, and Really Verifying Them
Gitfedericocalo.dev

At a Glance

An SSH signature can be correct and still untrusted: set up signed commits, read the G, U and N letters and write the allowed_signers file.

Who really wrote this commit?

In a commit, the author's name is a line of text that you type yourself. If I set user.email to your address, the log will show your name next to a change you never made, and Git has no objection. For a personal project this is not a problem. It becomes one when the repository feeds a release, a pipeline or a customer, and somebody has to be able to say "this commit was made by the person who claims to have made it". The answer is a signature: Git attaches a cryptographic signature to the commit, and the reader checks it against a list of keys they trust.

Since Git 2.34 (the version GitHub's documentation gives) the signature can use the same SSH key you already authenticate with, with no need to install GPG. The recipe in short: set gpg.format to ssh, point user.signingkey at your key, turn on commit.gpgsign so that every commit is signed, and write the keys you trust into an allowed_signers file, linked through gpg.ssh.allowedSignersFile. Then you look at the result with git log --show-signature or git verify-commit. The last part is the surprising one: a signature can be mathematically correct and still not trusted, and without the allowed_signers file Git cannot tell the two apart, so no commit is reported as verified.

In this article you will learn to configure signing, to read the letters Git prints for each commit, to write the signers file and to recognise the errors you meet on the first day. It is intermediate material: it assumes you know commits and Git configuration, and it serves anyone who works in a team or ships software. The trade-off, which I come back to at the end, is that a signature proves possession of a key and not the identity of a person: the rest is decided by whoever maintains the list of keys.

All the blocks were run on 2026-11-19 with Git 2.53.0 and OpenSSH 10.2p1, in a test repository created in a temporary folder outside the project. The keys were generated only for the test, with no passphrase, and deleted at the end; I used neither my own SSH agent nor my own configuration (GIT_CONFIG_GLOBAL pointed at a file in the test folder), and I ran no git push. $PROVA is that folder (the name is kept from the Italian run); where a path was long I replaced it with [...], and the identifiers in your repository will be different. The outputs are the real ones, from a run with LC_ALL=C, so Git prints its messages in English.

The problem, shown: you declare the author yourself

Before signing, look at what Git guarantees today. The first block reads two settings I have not touched yet and creates a commit with an invented author, passing name and address only for that call with -c. Both reads exit with code 1, meaning "not set", so the signature format and automatic signing start from their defaults.

The last line shows the result: the log reports "Maria Rossi" as the author, and the letter N after sig: says the commit has no signature at all.

An author declared in words, with no signature
git config --get gpg.format
# (exit 1)
git config --get commit.gpgsign
# (exit 1)
git -c user.name="Maria Rossi" -c user.email=maria@example.invalid commit -q -m "Commit with a declared author"
git log --format='%h %an <%ae> sig:%G?'
# 03f5679 Maria Rossi <maria@example.invalid> sig:N

The four settings, one by one

They are all git config variables: they live in .git/config if you set them in the repository, or in ~/.gitconfig with --global, and they are not imported from anywhere. This is what you need to know about each of them, according to the git config documentation and to what I verified:

  • gpg.format: chooses the signature type. It accepts openpgp, x509 and ssh; if you write nothing, openpgp applies, and the test above confirms it, since the variable was unset. With ssh the signing program is ssh-keygen (gpg.ssh.program).
  • user.signingkey: the key to use. With the SSH format it accepts the path of a private key, the path of a public key (if the private one is in the agent), or an inline public key with the key:: prefix. If it is missing, Git runs gpg.ssh.defaultKeyCommand and, without even that, stops with an error.
  • commit.gpgsign: when it is true every commit is signed; by default it is not, as the letter N in the previous block shows. You override it for a single commit with --no-gpg-sign.
  • gpg.ssh.allowedSignersFile: the path of the file with the trusted keys. It has no default value, and without it verification fails, as you will see shortly.

The reasons for these values: ssh avoids installing and configuring GPG, and reuses a tool already on your computer. I set commit.gpgsign to true because a signature you have to remember to ask for is a signature that is missing on the wrong commits. The path in user.signingkey has to be the one you really want, because Git does not check it until it signs.

In the documentation the public-key form requires the SSH agent; in my test, with no agent, the .pub path worked anyway because the matching private key sat next to it, and I cannot say whether that behaviour is guaranteed. If you do not use an agent, give the path of the private key.

Signing the first commit

The next block generates two ed25519 keys with no passphrase: one is mine, the other will simulate a stranger. Then it sets the four variables and builds the allowed_signers file with a single line, made of an identity (here the email from user.email) followed by the public key.

After making a commit, git cat-file -p HEAD shows how the signature lives inside the object: it is the gpgsig header, a block of base64 text delimited by BEGIN SSH SIGNATURE and END SSH SIGNATURE. The signature is not a separate file: it is part of the commit, so its identifier includes it.

Configuration and first signed commit
ssh-keygen -q -t ed25519 -N "" -C article-test-only -f $PROVA/test_key
ssh-keygen -q -t ed25519 -N "" -C stranger -f $PROVA/stranger_key
git config gpg.format ssh
git config user.signingkey $PROVA/test_key.pub
echo "$(git config user.email) $(cat $PROVA/test_key.pub)" > $PROVA/allowed_signers
git config gpg.ssh.allowedSignersFile $PROVA/allowed_signers
git config commit.gpgsign true
git commit -q -m "First signed commit"

git cat-file -p HEAD
# tree 8b6fa71282eead1f1399cd669f51f78592cfea3a
# parent 03f56794b2f752b4cd6de1b3c0ed363a1d89a6ec
# author Demo <demo@example.invalid> 1795099512 +0100
# committer Demo <demo@example.invalid> 1795099512 +0100
# gpgsig -----BEGIN SSH SIGNATURE-----
#  U1NIU0lHAAAAAQAAADMAAAALc3NoLWVkMjU1MTkAAAAg9e6GFGDhA6MdKmszzou5yPZ0dX
#  rJxNS4fSuequWBcEMAAAADZ2l0AAAAAAAAAAZzaGE1MTIAAABTAAAAC3NzaC1lZDI1NTE5
#  AAAAQOMYhXeGXr+n1wzlp1eM/qb7YMl3dfjOLE5egH/JJTjz7L0smSNj79iFoqR3yzuvre
#  lnCFow337cFhf20u14Uwk=
#  -----END SSH SIGNATURE-----
#
# First signed commit

Reading the result: G, U, N and the other letters

Three commands show the outcome. git log --show-signature adds ssh-keygen's verdict under each commit; git log --format with the placeholders %G? (status), %GS (signer) and %GK (key fingerprint) condenses it into one line, handy for scripts; git verify-commit checks a single commit and ends with an exit code you can use in a check.

In all three, the name that appears, demo@example.invalid, is the "principal" that the allowed_signers file associates with the key, not the commit's author field.

Three ways to read the same signature
git log --show-signature -1
# commit 4ab11195b33b98097bace756c4af76e0e711629d
# Good "git" signature for demo@example.invalid with ED25519 key SHA256:c8U+npDhP4tF5X4wFGH8XzGmcFb6HCV9ccl78NeSm9I
# Author: Demo <demo@example.invalid>
# Date:   Thu Nov 19 15:45:12 2026 +0100
#
#     First signed commit

git log --format='%h %G? %GS %GK' -1
# 4ab1119 G demo@example.invalid SHA256:c8U+npDhP4tF5X4wFGH8XzGmcFb6HCV9ccl78NeSm9I

git verify-commit HEAD
# Good "git" signature for demo@example.invalid with ED25519 key SHA256:c8U+npDhP4tF5X4wFGH8XzGmcFb6HCV9ccl78NeSm9I
# (exit 0)

The letter from %G? is the data that matters, and the git log documentation lists eight:

  • G: a good, valid signature;
  • B: a bad signature;
  • U: a good signature with unknown validity;
  • X: a good signature that has expired;
  • Y: a good signature made by an expired key;
  • R: a good signature made by a revoked key;
  • E: the signature cannot be checked, for example because the key is missing;
  • N: no signature.

In practice you will mostly meet G, N and U. The most insidious is U, because the word "Good" appears in the output even when the commit is not trusted, and whoever reads only that word stops too early. The next block shows it.

A good signature is not a trusted signature

Here I make two more commits: one signed with the stranger's key, which I have not put in allowed_signers, and one without a signature, obtained with --no-gpg-sign even though commit.gpgsign is on. The log tells them apart with different letters: U for the stranger, N for the unsigned one.

The stranger's commit has a cryptographically correct signature, so much so that verify-commit prints "Good", but the next line says "No principal matched": the key is not in the list of who may sign, and the command ends with code 1. For the unsigned commit verify-commit exits with code 1 and prints nothing, which is easy to miss if you only watch the screen output.

A stranger and an unsigned commit
git -c user.signingkey=$PROVA/stranger_key.pub commit -q -m "Signed by a stranger"
git commit -q --no-gpg-sign -m "Unsigned"

git log --format='%h %G? %s'
# 88f877a N Unsigned
# 2cd6440 U Signed by a stranger
# 4ab1119 G First signed commit
# 03f5679 N Commit with a declared author

git verify-commit HEAD~1
# Good "git" signature with ED25519 key SHA256:Jtnk5pnAi8BqsLST4gN0dDIO/eF9WHojYx9OeIHzMnM
# No principal matched.
# (exit 1)
git verify-commit HEAD
# (exit 1)

The documentation of gpg.ssh.allowedSignersFile explains why: SSH has no trust levels like GPG, so Git sets trust to "fully" only if the public key is present in the file; otherwise the level is undefined and git verify-commit and git verify-tag fail.

Trust, in other words, does not live in the signature: it lives in the list. Whoever controls that file decides who counts as a signer, which is why it is worth keeping it outside the working folder that a contributor can modify, or under review like any other security file.

When the signers file is missing

The most frequent case, on the first day, is forgetting gpg.ssh.allowedSignersFile. The block reproduces it by removing the variable. The result is worse than you would expect: git log prints an error and reports N even for commits that used to be G, and --show-signature writes "No signature" under a commit that does have one.

The error message is the only clue, and it appears in the middle of the commit list. If you notice that "all my commits show as unsigned", check this variable first, not the key.

Without the signers file everything looks unsigned
git config --unset gpg.ssh.allowedSignersFile
git log --format="%h %G? %s"
# 88f877a N Unsigned
# error: gpg.ssh.allowedSignersFile needs to be configured and exist for ssh signature verification
# 2cd6440 N Signed by a stranger
# error: gpg.ssh.allowedSignersFile needs to be configured and exist for ssh signature verification
# 4ab1119 N First signed commit
# 03f5679 N Commit with a declared author

git log --show-signature -1 HEAD~2
# error: gpg.ssh.allowedSignersFile needs to be configured and exist for ssh signature verification
# commit 4ab11195b33b98097bace756c4af76e0e711629d
# No signature
# Author: Demo <demo@example.invalid>
# Date:   Thu Nov 19 15:45:12 2026 +0100
#
#     First signed commit

git verify-commit HEAD~2
# error: gpg.ssh.allowedSignersFile needs to be configured and exist for ssh signature verification
# (exit 1)
git config gpg.ssh.allowedSignersFile $PROVA/allowed_signers

What is actually signed

To understand what the signature guarantees, it helps to take Git out of the picture and call ssh-keygen -Y directly. The block splits the commit into two files with a small awk script: sig with the signature, and payload with the rest of the object, which is exactly the text that was signed, without the gpgsig header.

Then ssh-keygen -Y find-principals asks who owns the key that signed, and -Y verify checks the signature with an identity and a namespace. The namespace is a string that prevents a signature from being reused in a different context: Git signs with git, and the same signature verified with file is rejected, with code 255.

The last test is the most instructive: I change a single character of the payload (I add a zero to the tree identifier) and verification fails with "incorrect signature". It is the guarantee you were looking for: the signature covers the file tree, the parent commit, author, committer and message. If someone changes even one of these elements, the signature no longer matches. Since the parent commit is part of the signed text, the signature indirectly protects the history underneath as well.

The same verification with ssh-keygen, and the altered payload
cat split.awk
# /^gpgsig /      { f = 1; sub(/^gpgsig /, ""); print > (d "/sig"); next }
# f && /^ /       { sub(/^ /, ""); print > (d "/sig"); next }
#                 { f = 0; print > (d "/payload") }
git cat-file commit HEAD~2 | awk -v d=$PROVA -f $PROVA/split.awk

head -1 $PROVA/sig; tail -1 $PROVA/sig; head -3 $PROVA/payload
# -----BEGIN SSH SIGNATURE-----
# -----END SSH SIGNATURE-----
# tree 8b6fa71282eead1f1399cd669f51f78592cfea3a
# parent 03f56794b2f752b4cd6de1b3c0ed363a1d89a6ec
# author Demo <demo@example.invalid> 1795099512 +0100

ssh-keygen -Y find-principals -f $PROVA/allowed_signers -s $PROVA/sig
# demo@example.invalid
ssh-keygen -Y verify -f $PROVA/allowed_signers -I demo@example.invalid -n git -s $PROVA/sig < $PROVA/payload
# Good "git" signature for demo@example.invalid with ED25519 key SHA256:c8U+npDhP4tF5X4wFGH8XzGmcFb6HCV9ccl78NeSm9I
ssh-keygen -Y verify -f $PROVA/allowed_signers -I demo@example.invalid -n file -s $PROVA/sig < $PROVA/payload
# Couldn't verify signature: namespace does not match
# Could not verify signature.
# (exit 255)

sed 's/^tree /tree 0/' $PROVA/payload > $PROVA/payload2
ssh-keygen -Y verify -f $PROVA/allowed_signers -I demo@example.invalid -n git -s $PROVA/sig < $PROVA/payload2
# Signature verification failed: incorrect signature
# Could not verify signature.
# (exit 255)

The errors you will meet on day one

Besides the missing file, three errors recur. In all three, signing does not start and the commit is not created: the block ends with a count from git log --oneline that stays at 4 after the three attempts, as proof that no commit was written.

With the key:: form and no SSH agent the message is "Couldn't get agent socket?": according to the documentation the inline form wants the private key in the agent, and the trailing question mark is part of the message. With a wrong path in user.signingkey the message names the file that will not open. With no key configured at all Git stops at once and asks you to set user.signingkey or gpg.ssh.defaultKeyCommand. In all three cases the exit code is 128, which in a script you can treat as "the commit does not exist".

Three ways of failing to sign
git config user.signingkey "key::$(cat $PROVA/test_key.pub)"
git commit -q -m "with key::"
# error: Couldn't get agent socket?
#
# fatal: failed to write commit object
# (exit 128)

git config user.signingkey $PROVA/missing.pub
git commit -q -m "missing key"
# error: Couldn't load public key [...]/missing.pub: No such file or directory?
#
# fatal: failed to write commit object
# (exit 128)

git config --unset user.signingkey
git commit -q -m "no key"
# fatal: either user.signingkey or gpg.ssh.defaultKeyCommand needs to be configured
# (exit 128)

git log --oneline | wc -l
# 4

Two more traps concern commits already made. The first: a change such as git commit --amend rewrites the commit, and if you give up the signature (here with --no-gpg-sign) the new commit shows as N; repeating the amend with signing active brings it back to G.

The second: the revocation file gpg.ssh.revocationFile, which holds public keys to be accepted no more. After I put my key in it, the same commit goes from G to B, with "Could not verify signature". The final command is an overall check you can use as an audit: it counts the letters across the whole history, and in my repository it gives two G, two N and one U.

Rewriting, revoking, counting
git commit -q -m "Sixth signed"
git log --format='%h %G? %s' -1
# 115a878 G Sixth signed
git commit -q --amend --no-edit --no-gpg-sign
git log --format='%h %G? %s' -1
# 064c308 N Sixth signed
git commit -q --amend --no-edit
git log --format='%h %G? %s' -1
# 115a878 G Sixth signed

awk '{print $1" "$2}' $PROVA/test_key.pub > $PROVA/revoked
git config gpg.ssh.revocationFile $PROVA/revoked
git log --format='%h %G? %s' -1
# 115a878 B Sixth signed
git log --show-signature -1 | head -3
# commit 115a87808a86f1f86c2852776b72990a9b3555ab
# Could not verify signature.
# Author: Demo <demo@example.invalid>

git config --unset gpg.ssh.revocationFile
git log --format='%G?' | sort | uniq -c
#       2 G
#       2 N
#       1 U

Signed tags, and what the server does

The article on annotated tags and SemVer left out git tag -s, because it needs a signing key. With this test's configuration it works like this: git tag -s creates a signed annotated tag and git tag -v verifies it.

An annotated tag without -s is not signed, and git verify-tag answers "no signature found" with code 1, even though commit.gpgsign is on: tags have a separate variable, tag.gpgSign, which I did not try.

One signed tag and one not
git tag -s v0.1.0 -m "First preview"
git tag -v v0.1.0
# Good "git" signature for demo@example.invalid with ED25519 key SHA256:c8U+npDhP4tF5X4wFGH8XzGmcFb6HCV9ccl78NeSm9I
# object 115a87808a86f1f86c2852776b72990a9b3555ab
# type commit
# tag v0.1.0
# tagger Demo <demo@example.invalid> 1795099512 +0100
#
# First preview

git tag -a v0.2.0 -m "Not signed"
git verify-tag v0.2.0
# error: no signature found
# (exit 1)

On the server the logic changes: local verification uses your allowed_signers, whereas GitHub compares the signature with the public keys you uploaded as a "signing key" in your account, and marks the commit "Verified" if the signature matches. Its documentation also lists an "Unverified" state for a signed commit whose signature cannot be verified, and lets administrators require signed commits on a branch. I ran none of this, because my tests touch no server: I report it from the documentation, consulted on 2026-11-19.

When it is worth it, and when it is not

The trade-off is twofold. The first: SSH signing is simpler than GPG, with fewer tools to install and a key you probably already have. The price is that SSH has no web of trust and no levels: everything rests on allowed_signers, which somebody has to distribute and keep up to date, whereas with GPG you can lean on keyservers and certifications.

The second: a signature proves possession of a key, not who the person is. If the key is stolen, the stolen commits show as G until you put it in the revocation file, and the effectiveness of the whole system depends on how fast you do so. For a personal repository with no consumers, signing can be an extra step with no benefit; for a project with releases or several people with write access, it is worth it.

Three limits must be stated. I tried only keys with no passphrase: a protected key needs the SSH agent, and I did not run that case, nor did I check how commit --amend and rebase behave with a timed agent. I did not try SSH certificates (the cert-authority option of the signers file). And a signature says nothing about code quality: a perfectly signed commit can contain a defect, and for that the pre-commit checks described in Git hooks remain. A minimal procedure to start, in the order it is best done:

  1. generate a key dedicated to signing, or decide to reuse the authentication one, and note its path;
  2. set gpg.format, user.signingkey and commit.gpgsign;
  3. create allowed_signers with one line per trusted signer and link it with gpg.ssh.allowedSignersFile;
  4. make a test commit and check that git log --format='%G?' gives G;
  5. only then, add the check to the pipeline or on the server.

The order matters because each step depends on the previous one: without the signers file, step four shows N and you might look for the error in the key, and a server-side check, put in place before you know that everything works locally, would block legitimate commits. And a signature on a small, clear change is worth more than a signature on a block of twenty files: keep your commits atomic before you sign them.

What you can do now

You can configure SSH signing with four variables and say what each accepts and what it means if you do not write it. You can read G, U, N and B, and you know why "Good" is not enough: trust comes from the allowed_signers file, which Git consults to decide who may sign.

You can reproduce verification with ssh-keygen -Y and see that the signature covers the whole commit: tree, parent, author and message. You recognise the three most common signing errors and the missing-file case that makes every commit look "unsigned". What remains outside is what I could not try here: the SSH agent with protected keys, tag.gpgSign, certificates and verification on a real server.

Frequently asked questions

How do you sign a commit with an SSH key?

Set git config gpg.format ssh and git config user.signingkey to the path of your key, then sign with git commit -S or turn on commit.gpgsign true. To verify you also need gpg.ssh.allowedSignersFile.

Why does Git say "Good signature" but verify-commit fails?

Because the signature is correct but the key is not in the allowed_signers file: the next line says "No principal matched", the status in %G? is U and the command ends with code 1.

Why do all my commits show as unsigned?

Almost always gpg.ssh.allowedSignersFile is missing, or the file does not exist: in the test Git marked even truly signed commits as N, with an error message in the middle of the list.

Sources

  • git-config - official Git documentation - gpg.format, gpg.ssh.allowedSignersFile (trust levels), gpg.ssh.revocationFile, gpg.ssh.defaultKeyCommand, user.signingKey, commit.gpgSign, tag.gpgSign; page opened with curl on 2026-11-19 (HTTP 200), text of the trust passage re-read; it matches the local git help -m config, Git 2.53.0
  • Local ssh-keygen manual (man ssh-keygen, OpenSSH 10.2p1) - -Y sign, -Y verify, -Y find-principals, namespaces, the "ALLOWED SIGNERS" section with the namespaces option; read on 2026-11-19 and re-tried with the commands of the direct-verification block (the web page man.openbsd.org/ssh-keygen.1 was opened with curl on 2026-11-19, HTTP 200, and the "ALLOWED SIGNERS" section matches)
  • About commit signature verification - GitHub Docs - SSH signing available from Git 2.34, key uploaded as a "signing key", "Verified" and "Unverified" states, required signatures on branches; page opened with curl on 2026-11-19 (HTTP 200), not tried on a server
  • gitformat-signature - official Git documentation - the signature format, which changes with gpg.format; page opened with curl on 2026-11-19 (HTTP 200), consulted only for the cross-reference from git config
  • Letters of %G? from the local git help -m log, consulted on 2026-11-19. Examples run with Git 2.53.0 and OpenSSH 10.2p1 in local test repositories on 2026-11-19, with LC_ALL=C for the English outputs.

Try These Tools

Free developer tools related to this topic.

Commenti

Caricamento commenti...

Sign in to comment

New articles and tools on Telegram too

The Telegram channel flags the site's new articles and tools.

Did you like this article?

Explore other content on the blog or discover my projects