How to edit an incorrect commit message in Git?

Asked

Viewed 31,330 times

26

How to edit an incorrect commit message in Git? Examples can be cited?

3 answers

41


There are 3 different situations, which become more and more complex:

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

  2. Edit older commits - BEFORE THE 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 go 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
    

    Alter pick for 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 messages, save and close.

  3. Change commits AFTER PUSH
    First of all, this is highly not recommended.
    This can break the respository and take a lot of work.
    99.9% of the time it’s best to leave the commit wrong.

    To change the history after push, just follow one of the above steps and then run:

    git push --force
    

Sources:
1. Git documentation
2. Github Guide - in English

11

git commit --Amend -m "New message"

9

Just perform a --amend:

git commit --amend

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

Browser other questions tagged

You are not signed in. Login or sign up in order to post.