TExcelTAD
Home › Git › Git Signing Commits

Git Signing Commits

Anyone can write any name and email into a commit: git config user.name "Linus Torvalds" takes one second. Signing adds a cryptographic signature to a commit or tag that proves it was made by the holder of a particular key. GitHub then shows a green Verified badge.

Authorship is just text

The author fields are whatever the committer's configuration says:

bash · try it yourself
mkdir demo && cd demo && git init -q
echo "hello" > a.txt && git add . && git commit -q -m "Honest commit"
echo "evil" >> a.txt
git -c user.name="Famous Maintainer" -c user.email=boss@example.org commit -q -am "Totally legit change"
git log --format='%h %an <%ae> %s'
echo "--- signature status (%G?):"
git log --format='%h %G? %s'
c0a9290 Famous Maintainer <boss@example.org> Totally legit change
4d20a75 Ada Lovelace <ada@example.com> Honest commit
--- signature status (%G?):
c0a9290 N Totally legit change
4d20a75 N Honest commit

%G? prints the signature status: G good, B bad, U good but unknown validity, N no signature. Both commits are unsigned, so nothing proves who made them.

Signing methods

Method Key type Setup effort Notes
SSH the SSH key you may already use for pushing lowest Git 2.34+; GitHub supports it
GPG (OpenPGP) a GPG key medium the traditional method
S/MIME (gpg.format x509) an X.509 certificate high corporate environments

Setting up SSH signing

bash
# 1. tell Git to sign with your SSH key
git config --global gpg.format ssh
git config --global user.signingkey ~/.ssh/id_ed25519.pub

# 2. sign every commit and tag automatically
git config --global commit.gpgsign true
git config --global tag.gpgsign true

# 3. on GitHub: Settings -> SSH and GPG keys -> New SSH key -> type "Signing key"

# signing explicitly instead
git commit -S -m "Signed commit"
git tag -s v1.0 -m "Signed release"

For GPG instead: gpg --full-generate-key, gpg --list-secret-keys --keyid-format=long, then git config --global user.signingkey <KEYID> (and leave gpg.format at its default, openpgp).

To verify SSH signatures locally, Git needs a list of trusted keys in an allowed signers file:

bash · try it yourself
mkdir -p ~/.config/git
cat > ~/.config/git/allowed_signers <<'EOF'
ada@example.com namespaces="git" ssh-ed25519 AAAAC3NzaC1lZDI1NTE5AAAAIExampleKeyOnlyForThisLesson
EOF
git config --global gpg.ssh.allowedSignersFile ~/.config/git/allowed_signers
git config --global gpg.format ssh
git config --global --get-regexp '^gpg\.'
gpg.ssh.allowedsignersfile /home/learner/.config/git/allowed_signers
gpg.format ssh

Where the signature lives

A signed commit's object contains an extra gpgsig header holding the signature over the rest of the commit. git cat-file -p HEAD on a signed commit shows something like:

tree 5b1e7e2f...
parent 9c3d2a1b...
author Ada Lovelace <ada@example.com> 1768471200 +0000
committer Ada Lovelace <ada@example.com> 1768471200 +0000
gpgsig -----BEGIN SSH SIGNATURE-----
 U1NIU0lHAAAAAQAAADMAAAALc3NoLWVkMjU1MTkAAAAg...
 -----END SSH SIGNATURE-----

Add signed feature

Because the signature is part of the commit, it changes the hash; amending or rebasing a signed commit drops the signature unless you sign again.

Verifying

Command Shows
git log --show-signature each commit with "Good signature from …"
git verify-commit <commit> verify one commit (exit status 0 = good)
git verify-tag v1.0 verify a signed tag
git merge --verify-signatures refuse to merge unsigned/untrusted commits

On an unsigned commit, verification simply fails with a non-zero status, which is how scripts and CI enforce signing:

bash · try it yourself
mkdir demo && cd demo && git init -q
echo "hi" > a.txt && git add . && git commit -q -m "Unsigned commit"
if git verify-commit HEAD 2>/dev/null; then
  echo "signature OK"
else
  echo "HEAD is not signed (or the signature is not trusted)"
fi
git cat-file -p HEAD
HEAD is not signed (or the signature is not trusted)
tree 0d8a474fc67971fb3dd7616e26323d3066442555
author Ada Lovelace <ada@example.com> 1768471200 +0000
committer Ada Lovelace <ada@example.com> 1768471200 +0000

Unsigned commit

The unsigned commit's object has no gpgsig header. On GitHub, branch protection can require signed commits on main.

Note: Signing proves which key made a commit, not that the code is good. Keep your private key safe (with a passphrase) and revoke it on GitHub if it leaks.

Test yourself

Why aren't the author name and email enough to prove who made a commit?

  1. They are encrypted
  2. Anyone can set them to any value in their Git config
  3. GitHub removes them
  4. They are only stored locally

Which option signs a single commit?

  1. git commit -s
  2. git commit -S
  3. git commit --verify
  4. git commit --gpg

What does %G? print for a commit with no signature?

  1. G
  2. B
  3. N
  4. U