1

Is there any way that I can run a script on a branch to reword all commit messages that contains a specific substring in the commit message. Say I have a repo like this:

enter image description here

Then I would like to reword all the commit messages (on mybranch and not main) that starts with build version and append the suffix ⚠️ (rebased since).

Can I do it via a git command, bash script or event typescript script (triggered by ts-node or deno)?

Norfeldt
  • 8,272
  • 23
  • 96
  • 152

3 Answers3

3

You can use git filter-repo as in this answer in order to modify commit messages.

See "Updating commit/tag messages" for a simple solution

If you want to modify commit or tag messages, you can do so with the same syntax as --replace-text, explained above. For example, with a file named expressions.txt containing

 build version==> build version ⚠️ (rebased since)

then running

git filter-repo --replace-message expressions.txt

But that will not append ⚠️ (rebased since) at the end of your commit message though.

If you need it at the end, then you need a commit-callback, as done in the answer I mentioned before.

VonC
  • 1,262,500
  • 529
  • 4,410
  • 5,250
0

a)

  • make an empty commit with a new message
  • squash it into old commit

b)

  • find out the needed depth

  • rebase it on itself interactively git rebase -i HEAD~3

  • change reworded commits to exec 310154e tsx reword-commit.ts

  • make reword-commit.ts rewrite the file with commit message

  • you can run that from TS via execa ow any outher ts shell launcher https://git-scm.com/book/en/v2/Git-Tools-Rewriting-History

Dimava
  • 7,654
  • 1
  • 9
  • 24
0

inspired from @VonC I came to this script:

(brew install git-filter-repo is needed)

#!/bin/bash

# Create a temporary file to store the commit messages
temp_file=$(mktemp)

main_head_hash=$(git rev-parse main)
suffix="⚠️ rebased since!"
# Use git log to retrieve the commit messages and store them in the temporary file
git log --pretty=format:%s $main_head_hash.. | grep -v $suffix | grep ' build version' > $temp_file

# Create a file to store the replacements
echo > replacements.txt

# Iterate over the commit messages in the temporary file
while read commit_message; do
  # Print the replacement message to the replacements.txt file
  echo "$commit_message==>$commit_message $suffix" >> replacements.txt
done < $temp_file


# ⚠️⚠️ Rewriting history ⚠️⚠️
git filter-repo --replace-message replacements.txt --force

# Remove the temporary files
rm $temp_file
rm replacements.txt

(The script was made with help of chatGPT - giving it clear instructions on what steps to write a script for. I'm aware of the temp. ban policy of chatGPT and hope this does not appear as an policy violation since the answer is not solely based of it, but derived from a conversation with trail and error - the script is verified to work and I hope that it will help others)

enter image description here

Norfeldt
  • 8,272
  • 23
  • 96
  • 152