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

Asked

Viewed 15,336 times

28

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

I would like to know how to edit the message that accompanies the commit, after he has been executed.

4 answers

34


It’s Tranquil William, do 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 isn’t the last one, you can edit an old commit using the interactive commit mode

git rebase -i

It’s also for if you’ve made any changes and want it to be part of the commit previous!

for example: You made a commit to close Issue X, but saw that something was missing, performed this operation, but do not want to have two commits closing the same Issue, so you merge the two into one commit using the amend

Ah and it only works right if you haven’t given push that commit

  • In case you want to fix old commits (that have already been sent to the server) just to be documented, it doesn’t happen then?

  • 1

    No... man. It doesn’t happen =\

  • 3

    @William rolling think it even rolls (with git rebase -i HEAD^10 and git push -f, for the last 10 commits), but in general is discouraged rewriting the history on the server -- will probably give problem in everyone’s Code. But maybe doing it in a separate branch, and syncing with everyone else later, it’s cool.

  • That part about what this on the server was more out of curiosity. Thanks.

22

Editing the last commit

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

Editing a commit on the timeline

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

git rebase -i

Your text editor will start. Change text to word pick for reword (or just 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 the file and close your editor. Your editor will start again, this time with the original commit message being modified. Modify it, save and close the editor.

Important point: you are rewriting history

So much for making a --amend simple or a reword with rebase you will be rewriting git history. That is, git will generate a new SHA1 for the commit. You nay will, for example, achieve a push because part of the original commit 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 amended.

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

The recommendation I leave is: Only write commits that aren’t on other trees (commits that haven’t been on a push yet). Otherwise know what you’re doing..

7

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

2

Another way to do this, maybe but simple, is with git-Gui. In the menu you can do Commit->Amend Last Commit. Then there will be a menu under which you can edit the last commit text; when you’re done, just press "Commit".

Browser other questions tagged

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