How to Update Git Commit Messages in Bulk

Ever needed to update multiple git commit messages at once?

Let’s say you messed up the tense structure. Instead of the commit following the “this commit will…” format, you wrote it in the “this commit…” format. It should’ve been “Adjust the heading color…” but you wrote “Adjusts the heading color…”.

This is a small difference, sure, but it’s the kind of difference that makes the lead dev who’s reviewing your merge request wrinkle his nose like he smelled something bad.

No matter how many commits have bad messages, the fix is easy. Start with this command, substituting x with the number of commits you’re going to adjust:

git rebase -i HEAD~x

This puts you into git’s rebase interface. The -i flag means you’re in interactive mode, which is key. You should see something like this when you hit Enter:

pick 5225f17 Refs #25499 - Adjust PressRelease header styles now that there's no Hero Image.
pick ea013cc Refs #26308 - Adjust the layout of LocatorMap on medium sized screens.

# Rebase 5225f17..ea013cc onto 5225f17 (2 commands)
#
# Commands:
# p, pick <commit> = use commit
# r, reword <commit> = use commit, but edit the commit message
# e, edit <commit> = use commit, but stop for amending
# s, squash <commit> = use commit, but meld into previous commit
# f, fixup [-C | -c] <commit> = like "squash" but keep only the previous
#                    commit's log message, unless -C is used, in which case
#                    keep only this commit's message; -c is same as -C but
#                    opens the editor
# x, exec <command> = run command (the rest of the line) using shell
# b, break = stop here (continue rebase later with 'git rebase --continue')
# d, drop <commit> = remove commit
# l, label <label> = label current HEAD with a name
# t, reset <label> = reset HEAD to a label
# m, merge [-C <commit> | -c <commit>] <label> [# <oneline>]
#         create a merge commit using the original merge commit's
#         message (or the oneline, if no original merge commit was
#         specified); use -c <commit> to reword the commit message
# u, update-ref <ref> = track a placeholder for the <ref> to be updated
#                       to this position in the new commits. The <ref> is
#                       updated at the end of the rebase
#
# These lines can be re-ordered; they are executed from top to bottom.
#
# If you remove a line here THAT COMMIT WILL BE LOST.
#
# However, if you remove everything, the rebase will be aborted.
#

At the start of each line, replace the word pick with r:

r 5225f17 Refs #25499 - Adjust PressRelease header styles now that there's no Hero Image.
r ea013cc Refs #26308 - Adjust the layout of LocatorMap on medium sized screens.

# Rebase 5225f17..ea013cc onto 5225f17 (2 commands)
#
# Commands:
# p, pick <commit> = use commit
# r, reword <commit> = use commit, but edit the commit message
# e, edit <commit> = use commit, but stop for amending
# s, squash <commit> = use commit, but meld into previous commit
# f, fixup [-C | -c] <commit> = like "squash" but keep only the previous
#                    commit's log message, unless -C is used, in which case
#                    keep only this commit's message; -c is same as -C but
#                    opens the editor
# x, exec <command> = run command (the rest of the line) using shell
# b, break = stop here (continue rebase later with 'git rebase --continue')
# d, drop <commit> = remove commit
# l, label <label> = label current HEAD with a name
# t, reset <label> = reset HEAD to a label
# m, merge [-C <commit> | -c <commit>] <label> [# <oneline>]
#         create a merge commit using the original merge commit's
#         message (or the oneline, if no original merge commit was
#         specified); use -c <commit> to reword the commit message
# u, update-ref <ref> = track a placeholder for the <ref> to be updated
#                       to this position in the new commits. The <ref> is
#                       updated at the end of the rebase
#
# These lines can be re-ordered; they are executed from top to bottom.
#
# If you remove a line here THAT COMMIT WILL BE LOST.
#
# However, if you remove everything, the rebase will be aborted.
#

From the help text included by git you can see that r is the reword option. It gives you the ability to edit the commit message without changing the code associated with the commit.

Save your changes and close out the editor interface (:w if you’re using vim like I am), and you’ll be presented with your commit messages, one after the other. Make your changes, then again close out the editor and save the change. When you run out of commits, you’ll leave the git interactive interface and be dropped back to the command line.

Hat tip to Jaco Pretorius, who put together a similar blog post which I used to fix this problem before. He made use of the edit command. I like reword better.


/