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

24

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

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

    
asked by Guilherme 17.12.2013 в 21:06
source

4 answers

25

It's Tranquilo Guilherme, do the following

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

This 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 commit mode

git rebase -i

It also works if you have made any changes and want it to be part of the commit previous!

For example:   You made commit to close the issue X, but you saw that something was missing, you did this, but you do not want to have two commits closing the same issue, so you merge the two in a single commit using amend

Oh, and this only works right if you have not yet given push of that commit

    
answered by 17.12.2013 / 21:08
source
22

Editing the latest commit

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

Editing a commit in the timeline

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

git rebase -i

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

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 save the file and close your editor. Your editor will start again, this time with the original commit message being modified. Modify, save, and close the editor.

Important point: you are rewriting history

Whether you are doing a single --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 not will be able to, for example, perform a push as part of the original commits tree is not present in your local branch.

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

>

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

The recommendation I leave is: Only re-write commits that are not in other trees (commits that have not yet been pushed). Otherwise know what you are doing. .

    
answered by 17.12.2013 в 22:00
7

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

    
answered by 17.12.2013 в 21:08
2

Another way to do this, perhaps simpler, is with git-gui . In the menu you can do Commit-> Amend Last Commit . From there will appear an under-menu where you can edit the text of the last commit; when you're done, just hit Commit.

    
answered by 30.01.2014 в 21:58