Signing Commits and Tags With GPG or SSH Keys
Prove that commits and tags really come from you: why author names prove nothing, signing with SSH keys or GPG, signing everything by default, verifying with log --show-signature, verify-commit and allowed signers files, Verified badges, and signing versus sign-off.
kvi5hnuCode Sprout team member7 min readOn this page
A commit's author name and email are just text, written by whoever made the commit. Anyone can create commits that claim to be from you. A signature fixes that: a cryptographic proof, made with a private key only you have, that you created this commit or tag and that it hasn't changed since. Hosting services show signed commits as Verified, and some projects require signatures on everything merged. This article covers signing with SSH keys, the simplest option today, and GPG, the traditional one.
Why names prove nothing
git init -q trail-guide && cd trail-guide
git commit -q --allow-empty -m "Add lake page"
git commit -q --allow-empty -m "Delete everything" --author="Ana Diaz <ana@example.com>"
git log --format='%h %an <%ae>: %s'
echo "=== signature status: N means no signature"
git log --format='%h %G? %s'
3d510d9 Ana Diaz <ana@example.com>: Delete everything
d7b060a Sam Lee <sam@example.com>: Add lake page
=== signature status: N means no signature
3d510d9 N Delete everything
d7b060a N Add lake page
Sam made both commits, but the second claims Ana as its author, and nothing in Git prevents that. Hosting services link commits to accounts by email, so the commit would even show Ana's profile picture. With signatures, Ana's real commits would carry Ana's signature and this one wouldn't.
Signing with an SSH key
Since Git 2.34, the same kind of key you use to connect over SSH can sign commits (article 21). It's the easiest way to start:
git config --global gpg.format ssh
git config --global user.signingKey ~/.ssh/id_ed25519.pub # the public key file; Git finds the private key next to it
git config --global commit.gpgSign true # sign every commit
git config --global tag.gpgSign true # sign every annotated tag
Then add the same public key to your hosting account as a signing key: on GitHub, in the SSH and GPG keys settings, choose "Signing Key" as the key type. A key can be added twice, once for authentication and once for signing.
Despite the gpg in their names, gpg.format, commit.gpgSign and tag.gpgSign apply to all signature formats. Signing uses ssh-keygen, and a key in ssh-agent (or a password manager acting as one) means no passphrase prompt per commit.
Verifying SSH signatures locally
GPG has a web of trust, but SSH keys don't, so Git needs a list of whose key is whose: an allowed signers file:
# ~/.config/git/allowed_signers
sam@example.com namespaces="git" ssh-ed25519 AAAAC3NzaC1lZDI1NTE5AAAAI...sam
ana@example.com namespaces="git" ssh-ed25519 AAAAC3NzaC1lZDI1NTE5AAAAI...ana
git config --global gpg.ssh.allowedSignersFile ~/.config/git/allowed_signers
Each line holds an email, the git namespace, and that person's public key. Teams often commit such a file to the repository, and point the setting at it.
Signing with GPG
GPG (GnuPG) signing has been supported for much longer, and its keys can expire, be revoked and be certified by others:
gpg --full-generate-key # choose ECC (Ed25519) or RSA 4096; use your commit email
gpg --list-secret-keys --keyid-format=long # find the key ID after "sec ed25519/"
git config --global user.signingKey 3AA5C34371567BD2
git config --global commit.gpgSign true
gpg --armor --export 3AA5C34371567BD2 # paste this public key into your hosting account
On macOS, install GnuPG and pinentry-mac so the passphrase prompt works outside a terminal. With Git for Windows, Git includes GPG, or point gpg.program at Gpg4win. A third format, gpg.format=x509, uses S/MIME certificates through gpgsm, for organizations that issue them.
Signing commits and tags
With commit.gpgSign set, every commit is signed. Without it, sign individually:
git commit -S -m "Fix lake distance" # sign this commit
git tag -s v1.0 -m "Version 1.0" # a signed annotated tag
git rebase --gpg-sign main # sign the commits a rebase creates
git commit --amend --no-edit -S # add a signature to the last commit
Rewriting commits, by amending, rebasing or cherry-picking, creates new commits, and they're signed only if signing is on, through commit.gpgSign or -S. A signed tag on a release is especially valuable: it vouches for the whole history up to that commit, because each commit ID depends on all the commits before it.
Verifying signatures
git log --show-signature -3 # signature details for each commit
git log --format='%h %G? %GS %s' # one-letter status, and the signer
git verify-commit HEAD # exit status 0 only if the signature is good
git verify-tag v1.0
git tag -v v1.0 # the same
git merge --verify-signatures feature # refuse to merge unless the branch tip is signed by a trusted key
The %G? status letters:
| Letter | Meaning |
|---|---|
G | good signature, by a trusted key |
U | good signature, but the key's trust is unknown |
B | bad signature: the commit was changed after signing |
X / Y | good signature, but the signature or the key has expired |
R | good signature by a revoked key |
E | can't check: the public key is missing, or the signing program isn't installed |
N | no signature |
Hosting services verify signatures against the keys in their users' accounts, and show Verified, Unverified or nothing. GitHub's vigilant mode marks unsigned commits that claim to be yours as Unverified too, which catches the forgery shown at the start. Branch protection rules can require signed commits.
Troubleshooting
When signing fails, Git can't write the commit:
git init -q trail-guide && cd trail-guide
echo "=== GPG format, but gpg isn't installed"
git commit --allow-empty -S -m "Add lake page"; echo "exit status $?"
echo "=== SSH format, but ssh-keygen isn't available"
git config set gpg.format ssh
git config set user.signingKey "ssh-ed25519 AAAAC3NzaC1lZDI1NTE5AAAAIexample"
git commit --allow-empty -S -m "Add lake page"; echo "exit status $?"
=== GPG format, but gpg isn't installed
error: cannot run gpg: No such file or directory
error: gpg failed to sign the data:
(no gpg output)
fatal: failed to write commit object
exit status 128
=== SSH format, but ssh-keygen isn't available
error: cannot run ssh-keygen: No such file or directory
error:
fatal: failed to write commit object
exit status 128
Common causes and fixes:
- The program isn't found. Install GnuPG or OpenSSH, or set
gpg.program(gpg.ssh.programfor SSH) to its full path. gpg failed to sign the datawith GPG installed: often the passphrase prompt can't appear. Runexport GPG_TTY=$(tty)in your shell's startup file, or install a graphical pinentry. Test withecho test | gpg --clearsign.- Signed, but not Verified on the hosting service: the commit email must be a verified email on your account, and the public key must be added as a signing key.
Eor "No principal matched" locally for SSH signatures: setgpg.ssh.allowedSignersFileand add the signer's line.
Signing is not sign-off
git commit -s (lowercase, --signoff) adds a Signed-off-by: Name <email> line to the message. It's a statement, used by projects with a Developer Certificate of Origin (DCO), that you have the right to submit the change. It's plain text and proves nothing cryptographically. -S (uppercase) makes a signature. Projects may ask for both.
What's current
- SSH signing (Git 2.34) is supported by GitHub, GitLab and Bitbucket, and is the simplest way to start signing.
- Hosting services can require signed commits on protected branches, and GitHub's vigilant mode flags unsigned commits that use your email.
- Since Git 2.54, a GPG signature made before its key expired is no longer shown as an alarming failure just because the key expired later.
- Commits made in hosting services' web interfaces, such as merges of pull requests, are signed by the service's own key.
Common questions
Do I need GPG to sign commits?
No. With gpg.format=ssh and user.signingKey pointing to an SSH public key, Git signs with ssh-keygen, and hosting services accept SSH signing keys.
Why does GitHub show my signed commit as Unverified? Usually the commit's email isn't a verified address on your account, or the public key isn't registered as a signing key there. For GPG keys, the key's email must also match.
Do I have to sign every commit?
That depends on the project. Setting commit.gpgSign=true is easiest, as it's automatic. Some projects only require signed release tags.
What happens to signatures when I rebase?
The rebased commits are new commits, so the old signatures don't apply to them. They're signed again if commit.gpgSign is set, or with git rebase --gpg-sign.
Summary
- Author names are unverified text. A signature proves who made a commit or tag, and that it's unchanged.
- SSH signing:
gpg.format ssh,user.signingKeyset to your public key, and the key added to your hosting account as a signing key. Verify locally withgpg.ssh.allowedSignersFile. - GPG signing: generate a key, set
user.signingKeyto its ID, and upload the public key. commit.gpgSignandtag.gpgSignsign by default, and-Sand-s(for tags) sign individually.git log --show-signature,%G?,verify-commitandverify-tagcheck signatures.-ssign-off is a text line for DCO, not a signature.
Practice questions
Set up Git to sign all your commits with your existing SSH key ~/.ssh/id_ed25519.
Answer
git config --global gpg.format ssh
git config --global user.signingKey ~/.ssh/id_ed25519.pub
git config --global commit.gpgSign true
Then add the contents of ~/.ssh/id_ed25519.pub to your hosting account as a signing key, and make sure your commit email is verified there.
git log --format='%h %G? %s' shows E for a teammate's commits. What does it mean, and how do you fix it?
Answer
E means Git couldn't check the signature: typically the teammate's public key isn't known locally, or the signing program is missing. For SSH signatures, add a line for their email and public key to your allowed signers file and set gpg.ssh.allowedSignersFile. For GPG, import their public key (gpg --import or from a key server).
What's the difference between git commit -s and git commit -S?
Answer
-s (--signoff) appends a Signed-off-by: line to the message, a text statement for projects using a Developer Certificate of Origin. -S (--gpg-sign) cryptographically signs the commit with your GPG or SSH key, which can be verified.
Review quiz
Test yourself on Part 8 with the Git Part 8 review quiz.
Previous: Tags and Releases: Lightweight and Annotated Tags, describe and archive

More in Git Course
Quiz: Git Delicate Details
Twenty-five questions on Git's subtle corners: what commit -a and stash leave out, empty folders, ignore patterns and negation, HEAD~ versus HEAD^, ours and theirs in a rebase, tags that don't move or don't push, describe, merge reverts, diff exit codes, stash conflicts and more.
Quiz: Git Interview Questions
Thirty Git questions of the kind asked in developer interviews: Git versus GitHub, the staging area, merge versus rebase, fetch versus pull, reset, revert and restore, detached HEAD, conflicts, force pushing, stash, cherry-pick, reflog, bisect, submodules, LFS and how commits are identified.
Quiz: Git Course Final Exam (All Parts)
The final exam for the Git course: thirty questions across all twelve parts, from the three areas and staging through branching, remotes, undoing, history investigation, configuration, tags, larger projects, patches, rewriting, internals and troubleshooting.
Quiz: Professional Git Practice (Git Part 12)
Sixteen questions on migrating to Git from Subversion, TFVC and Mercurial, team best practices for commits, branches and reviews, repository hygiene, and diagnosing and fixing the most common Git errors, with explanations.
Discussion
Sign in to ask a question or share what you learned.