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

  1. 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
    

    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
    
  2. 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
    
  3. Save and close this file.

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

  5. Push your changes with --force flag to force push:

$ git push --force <branch>

All done!