Sub topic 12
Rebasing, amending, and squashing signed commits
This chapter assumes you can already sign a commit, verify it with git log --show-signature, and that commit.gpgsign is set. If not, finish the chapters on configuration and signing first.
Why history rewrites break signatures
A signature covers the exact content of a commit object. That includes the tree, the parent hash, the author and committer lines, the timestamp, and the message. Change any of these and the old signature no longer matches.
Amending, rebasing, squashing, and cherry-picking all create new commit objects. Git does not copy the old signature to them. Each new commit is either signed again by you or left unsigned.
Amending
git commit --amend makes a new commit that replaces the last one. With commit.gpgsign true, it signs the result automatically. Without that setting, add -S:
git commit --amend --no-edit -SIf you amend a commit that someone else signed, the new commit carries your signature, not theirs. Authorship stays the same unless you pass --reset-author, so the author and the signer can differ.
Rebasing
git rebase replays each commit on a new base. In most Git versions, replayed commits are signed only if you ask. Automatic signing from commit.gpgsign applies to commits created by git commit, and rebase behavior has varied by version. The reliable approach is to request signing explicitly:
git rebase --gpg-sign mainThe -S short form works too, and it takes an optional key ID. Despite the name, --gpg-sign uses whichever format gpg.format selects, so it works for SSH keys as well.
To re-sign a branch without moving it, rebase onto its own merge base:
git rebase --gpg-sign --force-rebase $(git merge-base HEAD main)Rebase signs the commits it creates with your key. Commits you did not author will show your signature but their author name. Check that this is acceptable for your project before doing it to shared work.
Squashing
A squash, whether through git rebase -i or git merge --squash, produces one new commit. The signatures on the original commits are discarded, and the new commit is signed only by whoever creates it.
git merge --squash feature
git commit -S -m "Add feature"On hosting platforms, a squash merge made through the web interface is signed by the platform's own key. The commit then shows as Verified under the platform's identity, not yours. Check your platform's documentation if your project requires commits signed by the individual author.
Checking the result
After any rewrite, confirm every commit in the range is signed:
git log --show-signature main..HEAD
git log --format="%h %G? %GS" main..HEADThe %G? field shows one letter per commit. G means a good signature, U a good signature from an untrusted key, N no signature, B a bad signature, and E a signature that cannot be checked, for example because the key is missing. %GS prints the signer name.
Pushing rewritten history
Rewritten commits have new hashes, so a normal push is rejected. Use a lease-protected force push, and only on branches that others are not building on:
git push --force-with-lease origin featureTags need extra care. A signed tag points at one specific commit hash. If you rewrite that commit, the tag still points at the old one. Do not move a published tag. Create a new signed tag with a new version instead.
Common mistakes
| Symptom | Cause | Fix |
|---|---|---|
| Some commits show `N` after a rebase | Rebase did not sign the replayed commits | Re-run with `--gpg-sign` |
| Signature shows your name, not the author's | Rebase re-signed another person's commit | Expected, since you signed it. Avoid rewriting others' commits |
| Squash merge shows a different signer | The platform signed it | Squash locally and push, or accept the platform signature |
Exercise
You rebased a feature branch onto main. Running git log --format="%h %G?" main..HEAD prints N for every commit, although commit.gpgsign is true. What do you run to sign them, and why did they come out unsigned?
Show answer
Run git rebase --gpg-sign main (or -S). Rebase creates new commit objects and does not always apply commit.gpgsign, so the replayed commits were left unsigned. The explicit flag signs each new commit with your configured key.