What’s the fastest way to edit hundreds of Git commit messages?

That’s an old question but as there is no mention of git filter-branch, I just add my two cents.

I recently had to mass-replace text in commit message, replacing a block of text by another without changing the rest of the commit messages. For instance, I had to replace Refs: #xxxxx with Refs: #22917.

I used git filter-branch like this

git filter-branch --msg-filter 'sed "s/Refs: #xxxxx/Refs: #22917/g"' master..my_branch
  • I used the option --msg-filter to edit only the commit message but you can use other filters to change files, edit full commit infos, etc.
  • I limited filter-branch by applying it only to the commits that were not in master (master..my_branch) but you can apply it on your whole branch by omitting the range of commits.

As suggested in the doc, try this on a copy of your branch.
Hope that helps.


Sources used for the answer

  • Use case on when to use the function : https://git-scm.com/book/en/v2/Git-Tools-Rewriting-History#The-Nuclear-Option:-filter-branch
  • Function reference (with the list of options) : https://git-scm.com/docs/git-filter-branch
  • Examples of rewrite : https://davidwalsh.name/update-git-commit-messages

Leave a Comment

Hata!: SQLSTATE[HY000] [1045] Access denied for user 'divattrend_liink'@'localhost' (using password: YES)