How to edit an incorrect commit message in Git?


How to edit an incorrect commit message in Git? Can you cite examples?

asked by anonymous 05.05.2015 / 17:50

3 answers


There are 3 different situations, which are becoming increasingly complex:

  • Edit the last local commit - BEFORE PUSH :
    The git commit --amend will open your editor, with the content of the last commit message and you can edit it quietly.

  • Edit older commits - BEFORE PUSH :
    You will need to rebase your history, which is more complex than the previous process:

    $ git rebase -i HEAD~3 # Mostra a lista dos 3 últimos commits

    The list will look something like this:

    pick e499d89 Delete CNAME
    pick 0c39034 Better README
    pick f7fde4a Change the commit message but push the same commit.
    # Rebase 9fdb3bd..f7fde4a onto 9fdb3bd
    # 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

    Change pick to reword in the commits you want to edit the message:

    pick e499d89 Delete CNAME
    reword 0c39034 Better README
    reword f7fde4a Change the commit message but push the same commit.

    Save and close the file. After that git will open each of the commits marked with reword for editing. Edit the messages, save and close.

  • Change commits AFTER PUSH
    First of all, this is highly recommended . This can break the repository and give a lot of work.
    99.9% of the time it is better to leave the wrong commit.

    To change the history after the push, simply follow one of the above steps and then execute:

    git push --force
  • Sources:
    1. Git Documentation < br> 2. Github Guide in English

    05.05.2015 / 18:35

    Just perform a --amend :

    git commit --amend

    It will open the text editor with the message of the last commit and you can update the message.

    05.05.2015 / 17:52

    git commit --amend -m "New message"

    17.05.2018 / 15:05