Sub topic 12
Rebasing, amending, and squashing signed commits
This chapter assumes you can commit, tag, and read git log, and that you have already configured Git to sign (see the earlier chapters on configuration and verification). You should also know roughly what git rebase and git commit --amend do.
Why history rewriting breaks signatures
A commit signature covers the commit object. That object contains the tree, the parent commit IDs, the author and committer lines, and the message. Change any of these and the commit gets a new ID. The old signature no longer matches the new content, so it cannot be carried over.
Every rewriting command creates new commits. Whether those new commits are signed depends on your configuration and on which command created them. Git never copies a signature from an old commit to a new one.
The table below shows what happens to the signature for common operations.
| Operation | New commit created? | Signed? |
|---|---|---|
| `git commit --amend` | Yes | Signed if `commit.gpgsign` is true or you pass `-S` |
| `git rebase` (commits replayed) | Yes, for every replayed commit | Only if signing is configured or you pass `-S` |
| `git rebase` (commits not changed, fast-forwarded) | No | Original signature stays valid |
| `git cherry-pick` | Yes | Signed if `commit.gpgsign` is true or you pass `-S` |
| `git merge` (true merge) | Yes, a merge commit | Signed if `commit.gpgsign` is true or you pass `-S` |
| `git merge --ff-only` | No | Original signatures stay valid |
| `git commit --squash` / `--fixup` | Yes | Signed if configured |
Amending a signed commit
When you amend, Git builds a replacement commit and moves the branch to it. With commit.gpgsign set to true, the replacement is signed with your current key automatically.
git commit --amend --no-edit
git log --show-signature -1Without commit.gpgsign, the amended commit is unsigned, even if the original was signed. Pass -S to sign it explicitly:
git commit --amend --no-edit -STo remove a signature on purpose, use --no-gpg-sign:
git commit --amend --no-edit --no-gpg-signGit also uses the key from user.signingkey. If you changed that setting since the original commit, the amended commit is signed by the new key. The original signature is gone either way.
Rebasing signed commits
A rebase replays your commits on top of a new base. Each replayed commit has a new parent, so each gets a new ID and loses its signature. This applies even if you never touched the content of that commit.
To re-sign every replayed commit, pass -S to the rebase:
git rebase -S mainIf commit.gpgsign is true, Git signs replayed commits without the flag. Check the result:
git log --show-signature main..HEADYou can also re-sign commits after the fact. This replays every commit from the base and signs each one:
git rebase --exec 'git commit --amend --no-edit -n -S' -i mainThe --exec option runs the command after each replayed commit. The -n flag skips pre-commit and commit-msg hooks during the amend, which avoids running them twice.
Signing and the committer identity
The signature is made by the committer, not the author. A rebase keeps the original author but sets you as the committer. If you rebase a colleague's commits, the new commits show their name as author and yours as committer, and your key signs them. Hosting platforms usually check the signature against the committer email, so the badge reflects you, not the original author.
This is accurate: you did create these new commits. But it means a signature on a rebased commit says "this person rewrote the commit", not "this person wrote the change".
Squashing
A squash combines several commits into one. The result is a new commit, so the signatures on the originals are discarded. You must sign the combined commit yourself.
git rebase -i mainIn the todo list, mark the commits to combine with squash (or s) or fixup (or f). Git then creates the combined commit. With commit.gpgsign true, it is signed. If not, use -S on the rebase command.
You can also squash with a soft reset:
git reset --soft main
git commit -S -m "Add release notes"git reset --soft moves the branch pointer but keeps all changes staged. The new commit is a fresh object with a fresh signature.
Squash merges on hosting platforms
When you merge a pull request with "Squash and merge", the platform creates the new commit on its server. The signature on that commit depends on the platform, not on your key. Many platforms sign such commits with their own web-flow key, and show them as verified because of that key. Your original signed commits in the branch are not part of the resulting history on the target branch. Check your platform's documentation for what it does, because behavior differs.
Rebase conflicts and signing
If a rebase stops on a conflict, you resolve it and run git rebase --continue. Git then creates the commit and signs it as usual. If signing fails at that point, for example because the passphrase prompt cannot appear, the rebase stops with an error. Fix the signing problem first (see the chapter on troubleshooting), then run git rebase --continue again. You do not need to restart the rebase.
export GPG_TTY=$(tty)
git rebase --continuePushing rewritten history
Rewriting changes commit IDs, so pushing needs a force. Use the safer form:
git push --force-with-lease--force-with-lease refuses to push if the remote branch has moved since your last fetch. A plain --force would overwrite other people's work.
Only rewrite branches that nobody else builds on. Rewriting a shared branch forces everyone else to reconcile their local copies. Protected branches often reject force pushes altogether.
Tags and rewriting
A signed tag points at one commit and signs that commit's ID. If you rewrite the commit, the tag still points at the old commit, which remains reachable through the tag. The tag signature stays valid, but the tag no longer sits on your rewritten branch.
To move a tag, you must create a new one, since the old signature belongs to the old tag object:
git tag -s -f v1.0.0 -m "Release 1.0.0" HEAD
git push --force origin v1.0.0Do not move a published tag that others have already used. Consumers who verified the earlier tag will see a different object. Publish a new version tag instead.
Verifying after a rewrite
After any rewrite, check that every commit in your branch is signed:
git log --format='%h %G? %an %s' main..HEADThe %G? placeholder prints a one-letter status:
| Letter | Meaning |
|---|---|
| `G` | Good signature |
| `B` | Bad signature |
| `U` | Good signature, unknown validity |
| `X` | Good signature that has expired |
| `Y` | Good signature made by an expired key |
| `R` | Good signature made by a revoked key |
| `E` | Signature cannot be checked, for example a missing key |
| `N` | No signature |
Any N in your branch means a commit lost its signature during the rewrite. Re-sign it with the --exec approach shown above.
For SSH signatures, U often appears when the signer is not in the allowed signers file. That is covered in the SSH verification chapter.
Common mistakes
- Assuming a rebase keeps signatures. It does not. Check with
%G?afterward. - Rebasing someone else's commits and expecting their signature to remain. The new commits carry your signature, not theirs.
- Forgetting
GPG_TTYin a shell where the signing prompt cannot show, which makesgit rebasefail halfway. - Using
git push --forceinstead of--force-with-lease. - Moving a published tag instead of releasing a new version.
Exercise
You have three signed commits on a feature branch. You run git rebase main with commit.gpgsign unset, and then git log --format='%h %G?' main..HEAD. What status letter do you expect for the three commits, and what command would re-sign them all?
Show answer
You expect N (no signature) for each replayed commit, because the rebase created new commits and signing was not enabled. Re-sign them with git rebase --exec 'git commit --amend --no-edit -n -S' -i main, or redo the rebase with git rebase -S main. Then push with git push --force-with-lease.
Exercise
A teammate asks why a commit they authored shows your name as committer after you rebased their branch, and whose key signed it. What is the answer?
Show answer
A rebase keeps the original author but creates a new commit with you as committer. The signature is made with the key of the person running the rebase, so your key signed it. The signature says you rewrote the commit, not that you wrote the change.