DEV Community

Serhat Teker
Serhat Teker

Posted on • Originally published at tech.serhatteker.com on

How to Change the Message of Older Git Commits

If you need to change the message for multiple commits or an older commit, you can use interactive rebase:

  • α. Use git rebase -i HEAD~n command to display a list of the last n commits in your default text editor.
# Displays a list of the last 5 commits on the current branch
$ git rebase -i HEAD~5
Enter fullscreen mode Exit fullscreen mode

The list will look similar:

pick 9a55d9f Fix #3472
pick c174ecc Update README
pick b1c10fb Bad commit message
pick be26909 WTH
pick b723070 WIP

# Rebase 8de6748..b723070 onto 8de6748
#
# 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
Enter fullscreen mode Exit fullscreen mode
  • β. Replace pick with reword before each commit message you want to change. As an example I'm going to change my last 3rd, 4, 5th commit messages:
pick 9a55d9f Fix #3472
pick c174ecc Update README
reword b1c10fb Bad commit message
reword be26909 WTH
reword b723070 WIP
Enter fullscreen mode Exit fullscreen mode
  • γ. Save and close this file.

  • δ. In each related commit file, type the new message, save the file and close it.

  • ε. Push your changes with --force flag to force push:

$ git push --force <branch>
Enter fullscreen mode Exit fullscreen mode

All done!

Top comments (0)