blog.fidelramos.net<br>– Custom git trailers with Magit
Fidel Ramos
Git trailers are just lines of text at the bottom of a commit that take note of some<br>metadata. Common ones are Co-Authored-By, Reported-By, Reviewed-By or<br>Signed-Off-By, all of them autodescriptive I think.
Magit has a way of adding trailers, just press C-c TAB or C-c C-i in commit<br>view. Magit will then display a transient menu with some options:
Magit insert trailer menu
Nowadays it's becoming commonplace to use the Assisted-by:<br>trailer to credit AI coding agents,<br>and I wanted to start using it as I'm doing most of my coding with AI support now.<br>Magit doesn't support this trailer (yet!), but I had my AI figure out how to add it.
In this small HOWTO I will show you how to add a new kind of commit trailer to Magit, so<br>it will be just a C-c TAB away. I will use the Assisted-by example, but you can<br>tweak it to anything you need. It prompts for a value (free text, e.g. Kimi K3 or<br>venice/zai-org-glm-5-2) and appends it to the commit buffer as Assisted-by:<br>after any other existing trailers.
It remembers previously entered values, which are offered as completion. As a bonus I<br>will show how to configure Emacs' built-in savehist package to persist that history<br>across Emacs sessions.
Magit config
(use-package magit<br>:preface<br>(defvar my-git-commit-assisted-history nil<br>"History of values entered for `my-git-commit-assisted'.")<br>(defun my-git-commit-assisted ()<br>"Insert an \"Assisted-by\" trailer crediting an AI coding agent.<br>Prompt for the agent description (free text, e.g. \"Claude Opus 4.7\"),<br>offering previously entered values as completion."<br>(interactive)<br>(let ((value (completing-read "Assisted by: "<br>my-git-commit-assisted-history<br>nil nil nil<br>'my-git-commit-assisted-history)))<br>(when (string-empty-p value)<br>(user-error "Empty agent description"))<br>;; NOTE: `git-commit--insert-trailer' is a private API; magit<br>;; offers no public function for inserting an arbitrary trailer.<br>(git-commit--insert-trailer "Assisted-by" value)))<br>:config<br>;; C-c TAB A or C-c C-i A inserts the trailer<br>(transient-append-suffix 'git-commit-insert-trailer 'git-commit-test<br>'("A" "Assisted" my-git-commit-assisted))<br>(add-to-list 'git-commit-trailers "Assisted-by"))
Copy
How it works:
transient-append-suffix appends the command to<br>git-commit-insert-trailer (C-c TAB) right after the built-in<br>"Co-developed" entry, so it shows up as A in the trailer menu.
Prompt for the value happens by completing-read in the minibuffer, so you get your<br>configured completion system (I use vertico) to<br>pick among the candidates, or input free text.
Insertion goes through the same internal helper magit uses for<br>Signed-off-by, so the trailer lands after existing trailers and<br>before the comment/diff section regardless of buffer layout.
Adding "Assisted-by" to git-commit-trailers makes the token<br>font-lock like the built-in ones. This is needed because values are<br>free text; magit only highlights arbitrary tokens when they have the<br>Name shape.
A note for old Magit users: the trailer menu used to be<br>git-commit-insert-pseudo-header; it is now an obsolete alias for<br>git-commit-insert-trailer.
Bonus: remember values across sessions with savehist
Out of the box the completion history dies with the Emacs session, which is unfortunate<br>because most of the time we will be entering the same values again and again. By<br>enabling savehist-mode, every minibuffer history variable is saved automatically once<br>used — this history included — so often nothing extra is needed besides having the mode<br>enabled. If you'd rather state the intent explicitly (or you save only a whitelist by<br>setting savehist-save-minibuffer-history to nil), register it:
(use-package savehist<br>:config<br>(setq savehist-additional-variables<br>'(my-git-commit-assisted-history))<br>(savehist-mode +1))
Copy
Ordering matters: savehist-mode restores once when enabling it, so if you put it<br>before setting savehist-additional-variables then you would see the new variables<br>(my-git-commit-assisted-history in my case) get stored in the savehist file, but<br>they would not be restored when Emacs starts again. Ask me how I know.
Closing thoughts
I dislike having to use a private var like git-commit--insert-trailer, it's something<br>that might break at any point. But I'm OK with it because it's very localized and in a<br>non-critical functionality that I won't mind if it ever breaks. If it happens I will<br>remember to update this post.
About Assisted-by, I plan on using it extensively, I want to be very honest about my<br>AI use.
Are you using Assisted-by or other commit trailers?
aiemacsfree-softwarehowtoprogramming
Please enable JavaScript to view comments.