Apparently not the most popular situation, but I have a number of commits to whose messages I want to append a line Reviewed-by: user<mail>
.
So far I only found this command which fails for me with Invalid line: 10: Reviewed-by: User <mail>
GIT_EDITOR='git interpret-trailers --trailer "Reviewed-by: User <mail>" --in-place' git rebase -i HEAD~8
I also asked on IRC, to no avail.
Any other suggestions are welcome.
git rebase
solutionFor git 2.32 and higher:
git rebase HEAD~3 -x 'git commit --amend --no-edit --trailer "Reviewed-by: User <email>"'
For older versions I had the following solution. It doesn't look perfect, but does its job, and in my sanity-test it doesn't fail e.g. with commit messages that has a single double quote:
git rebase HEAD~2 -x 'git commit --amend -m"$(git log --format=%B -n1)$(echo -ne \\nReviewed-by: User \<email\>.)"'
Let's break down the second command (which is a complicated version of the 1st one):
git rebase HEAD~2 -x …
does rebase of n commits (where n = 2), to which you wanted to append Reviewed-by
, and for every commit it stops and executes the shell command that follows.git commit --amend -m…
amends the commit, and replaces its message with the one that follows.git log --format=%B -n1
prints message of the "current" commit.echo -ne \\nReviewed-by: User \<email\>.
this one is a bit tricky. The obvious part is that it adds the Reviewed-by
text. The less obvious one is that inline command execution removes trailing whitespace, i.e. the output of the previous command don't have newlines. So echo
here adds a newline, and then the text.You can use that as is by getting it from command history, or you can wrap it to a bash/zsh function, e.g.:
# adds reviwed-by to n commits
function git_rb() {
# export arguments, otherwise they're not visible to inline shell executions
export who=$1
export email=$2
export n=$3
git rebase HEAD~$n -x 'git commit --amend -m"$(git log --format=%B -n1)$(echo -e \\nReviewed-by: ${who} \<${email}\>.)"'
}
and then use it as
$ git_rb "Holy Moly" "Holy@example.com" 5
git filter-branch
solutionA bit limited solution that I found in this article. Turns out, you can use git filter-branch
to do the same. It prints a warning though that says it can mangle up the history, and recommends to use git filter-repo
instead. But it's a separate from git project, I didn't test it. From reading "gotchas" I got an impression that for modifying commit messages git filter-branch
is fine. But one problem I find with this method: you can't use a HEAD~n
notation, it would bail out with You must specify a ref to rewrite
error. Instead you have to use a notation like master..HEAD
, which does limit its usefulness a bit. An example:
git filter-branch -f --msg-filter 'cat && echo -e "\nReviewed-by: User<Holy Moly>"' master..HEAD