I have to replace a text with another one in all the git repo commit messages.
It seems to be possible with git rebase -i ... that opens a text editor but I have to do it automatically.
Is it doable with some of the git commands or maybe with a Java library?
2 Answers 2
git filter-branch is the tool to use for automatic bulk history rewriting.
Specifically --msg-filter:
--msg-filter This is the filter for rewriting the commit messages. The argument is evaluated in the shell with the original commit message on standard input; its standard output is used as the new commit message.
In your case, a simple sed as the command might suffice.
Comments
git filter-branch is not recommended.
Use an alternative history filtering tool such as git filter-repo.
git filter-repo --message-callback 'return re.sub(b"#", b"GH-", message)' --force --replace-refs delete-no-add
git filter-branchas you essentially get to execute a script per commit, and can change all the metadata in there. Think is the operative word, so I'm leaving this as just a comment.rebaseyou can do something likegit rebase -i -x "git commit --amend --message yourmessage" refspec. This will allow you to execute arbitrary commands and change any messages.git rebaseorgit filter-branchor any other tool—what you're doing is not changing an old commit, but rather, copying that old commit to a new (but different and improved) one. To use the new and improved commit easily, you also then copy any and all subsequent commits.