3

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?

asked Oct 23, 2020 at 7:48
4
  • 1
    I think you can do that with git filter-branch as 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. Commented Oct 23, 2020 at 7:56
  • If you want to use rebase you can do something like git rebase -i -x "git commit --amend --message yourmessage" refspec. This will allow you to execute arbitrary commands and change any messages. Commented Oct 23, 2020 at 7:57
  • @LasseV.Karlsen Yeah. I've used it already. There seems to be a tool that can achieve it by using filter-branch behind the coulisses github.com/da-x/git-search-replace But I've not used it yet. Commented Oct 23, 2020 at 7:58
  • Note that in all cases—whether using git rebase or git filter-branch or 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. Commented Oct 23, 2020 at 19:28

2 Answers 2

5

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.

answered Oct 23, 2020 at 7:59
Sign up to request clarification or add additional context in comments.

Comments

0

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
answered Jun 25, 2024 at 19:58

Comments

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.