In git, how do I edit the description of a commit already made?

In case you need to change the description of a commit, to make the description clearer, or specify which issue it is bound to.

I would like to know how do I edit the message that comes with commit, after it has been executed.

 27
Author: Lucas Blaschi Gameiro, 2013-12-17

4 answers

It's quiet William, does the following

git commit -m "Nova mensagem que vai substituir a anterior" --amend 

With this you will overwrite the old message of your commit!

And even if the commit is not the last one, you can edit an old commit using the interactive mode of commit

git rebase -i

Is also suitable if you have made any changes and want them to be part of the previous commit!

For example: You did a commit to close issue X, but saw that something was missing, performed this operation, but you don't want to have two commits closing the same issue, so you merge the two into one commit using the amend

Oh and this only works right if you haven't given push that commit

 33
Author: Luiz Carvalho, 2013-12-18 18:26:32

Editing the last commit

Just do git commit --amend -m "nova mensagem".

Editing a commit in timeline

If the commit you want to edit is not the last one you can edit it via rebase interactive:

git rebase -i

Your text editor will start. Change in the text the word pick to reword (or only r) of the desired commit, example:

pick fef7501 Primeiro commit.
reword 90d77f4 Segundo commit.
pick b82a17f Terceiro commit.

# Rebase 3620553..b82a17f onto 3620553
#
# 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

Save and file and close your editor. Your editor will start again, this time with the message original commit to be modified. Modify it, save and close the editor.

Important point: you are rewriting history

Either by making a simple --amend or a reword with rebase you will be rewriting the git history. That is, git will generate a new SHA1 for the commit. You will not be able, for example, to perform a push because part of the original commits tree is not present in your local branch.

In the case of rebase all commits that are part of the rebase will be rewritten (new SHA1) even if not changed.

Unless you force the push (git push -f) git rejects commits that rewrite your history.

The recommendation I leave is: only rewrite commits that are not in other trees (commits that have not yet been in a push). Otherwise know what you're doing..

 22
Author: talles, 2013-12-17 21:00:37

If it is the last commit, use git commit --amend -m "Nova mensagem". It is not recommended to do this if you have already given a push from commit.

 7
Author: rodrigorgs, 2013-12-17 20:08:34

Another way to do this, perhaps but simple, is with git-gui. In the menu you can make Commit->Amend last Commit. Then a sub-menu will appear where you can edit the text of the last commit; when finished, just press "Commit".

 2
Author: sturmer, 2014-01-30 20:58:55