Git: Rewriting commit messages through a rebase

So I accidentally failed in entering a correctly spelled commit message and pushed it. Someone saw it and I felt I wanted to redo it. If it just the latest commit then no problem, you just do an amend, but I had committed once more afterwards. And pushed.
Since I was still in my own branch the solution was to do an interactive rebase and then use reword. So instead of using the pick command I use reword . I start by executing the rebase command with the interactive flag (-i):

git rebase -i master

Then you will see the interactive feature where you can edit your commits. This may be what it looks like:

pick 0db9efc MAF-105: A fantatsic fix!

# Rebase 552b628..3bc00d5 onto 552b628
#
# Commands:
#  p, pick = use commit
#  r, reword = use commit, but edit the commit message
#  e, edit = use commit, but stop for amending
#  s, squash = use commit, but meld into previous commit
#  f, fixup = like "squash", but discard this commit's log message
#  x, exec = run command (the rest of the line) using shell
#
# 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.
#
# Note that empty commits are commented out

Now change the command pick to reword and alter the text. Then you just continue your rebase and your commit comment is changed, like this:

reword 0db9efc MAF-105: A fantastic fix! It's so good!

# Rebase 552b628..3bc00d5 onto 552b628
#
# Commands:
#  p, pick = use commit
#  r, reword = use commit, but edit the commit message
#  e, edit = use commit, but stop for amending
#  s, squash = use commit, but meld into previous commit
#  f, fixup = like "squash", but discard this commit's log message
#  x, exec = run command (the rest of the line) using shell
#
# 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.
#
# Note that empty commits are commented out

Let the rebase finish and you’re done!

Leave a Reply