Add trailers during a rebase
Add a co-author trailer to each replayed commit with git rebase, and skip duplicate lines with addIfDifferent.
You paired on a branch, and the project wants a Co-authored-by line on every commit before it merges. Amending five commits by hand, or marking each one reword in an interactive rebase, means five rounds of editing the same line.
One flag for the whole branch
Git 2.54 gave git rebase a --trailer option. It appends the trailer to every commit it rebases and formats it with the same rules as git interpret-trailers.
$ git log --oneline main..feature
e3f024e Add step 3
93f38e1 Add step 2
5ff2ebf Add step 1
$ git rebase --trailer "Co-authored-by: Ana Lima <ana@example.com>" main
Current branch feature is up to date, rebase forced.
Successfully rebased and updated refs/heads/feature.
$ git log --format="%h %s%n%(trailers)" main..feature
a0abf8f Add step 3
Co-authored-by: Ana Lima <ana@example.com>
a456df6 Add step 2
Co-authored-by: Ana Lima <ana@example.com>
f7c40f7 Add step 1
Co-authored-by: Ana Lima <ana@example.com>All three commits now end with the trailer, and all three have new hashes.
The branch already sat on top of main, and Git rebased it anyway. The rebase docs say --trailer implies --force-rebase, so Git replays the selected commits even when the branch already sits on its upstream. For a branch you already pushed, coordinate the history rewrite before updating the remote with git push --force-with-lease.
The upstream argument picks the range. git rebase --trailer "Signed-off-by: Sam Doe <sam@example.com>" HEAD~2 stamps only the last two commits. Repeat the flag to add two trailers in one run.
Reruns and duplicates
A second run can leave the trailer block unchanged while still replaying commits. The default, trailer.ifexists=addIfDifferentNeighbor, skips an identical trailer if it sits next to the insertion point.
That check only looks at the neighbour. A commit whose message already had Ana's line followed by a Reviewed-by line got a second copy:
$ git rebase --trailer "Co-authored-by: Ana Lima <ana@example.com>" main
$ git log -1 --format="%(trailers)" HEAD~1
Co-authored-by: Ana Lima <ana@example.com>
Reviewed-by: Kim Park <kim@example.com>
Co-authored-by: Ana Lima <ana@example.com>
$ git reset --hard ORIG_HEAD
$ git -c trailer.ifexists=addIfDifferent rebase --trailer "Co-authored-by: Ana Lima <ana@example.com>" main
$ git log -1 --format="%(trailers)" HEAD~1
Co-authored-by: Ana Lima <ana@example.com>
Reviewed-by: Kim Park <kim@example.com>With addIfDifferent, Git searches the whole trailer block for the same key and value before adding the line. Existing duplicates stay in the message.
The reset in the example uses a clean scratch repository immediately after the rebase. It discards uncommitted work, and other Git commands can overwrite ORIG_HEAD. To keep a recovery point on a real branch, create a backup ref before rebasing.
What Git will not stop
Git accepts a trailer without a separator and adds an empty value. Check the trailer text in git log before you push.
The option needs the merge backend. git rebase --apply --trailer … fails with fatal: --trailer requires the merge backend.
On Git 2.32 through 2.53, use the git commit --trailer option in an exec line:
git rebase -x 'git commit --amend --no-edit --allow-empty --trailer "Co-authored-by: Ana Lima <ana@example.com>"' mainGit amends each commit right after replaying it. --allow-empty also covers commits that have a message and no file changes.