2

As shown on this popular answer in SO, when I need to edit/correct the message for the last commit, I do:

git commit --amend -m "New commit message"

But what am I supposed to do when I want to amend commit messages for commits previous to the last one? For instance, the 14th last commit - also considering that commits have not been pushed to remote?

Is there a way of doing this without resetting previous commits? Would rebase be the right thing to do in this case?

Community
  • 1
  • 1
Wallace Sidhrée
  • 11,221
  • 6
  • 47
  • 58
  • Possible duplicate of [How do I edit an incorrect commit message in Git?](http://stackoverflow.com/questions/179123/how-do-i-edit-an-incorrect-commit-message-in-git). Among the answers to the linked question, you'll find [this one](http://stackoverflow.com/a/180085/456814), which basically says to use interactive rebase. –  Mar 19 '14 at 01:30

1 Answers1

5

You would need interactive rebasing as mentioned here: How to modify a specified commit in git?

The difference being, instead of edit you'll just want to reword the commit in question since you just want to edit the commit message.

Here's another useful resource: https://help.github.com/articles/interactive-rebase

edit: reword only works on versions 1.6.6 or newer as noted by the OP.

Community
  • 1
  • 1
Ayush Chaudhary
  • 716
  • 1
  • 8
  • 20
  • 3
    Be careful not do this when your commits have been pushed to a remote repo. http://www.git-scm.com/book/en/Git-Branching-Rebasing#The-Perils-of-Rebasing – Schleis Mar 18 '14 at 18:15
  • True. Like the GitHub link says - "Doing so may invoke the wrath of the git gods" – Ayush Chaudhary Mar 18 '14 at 18:17
  • It works great, thanks! One thing though, after some further reading: `reword` can only be used in `Git 1.6.6` and newer. – Wallace Sidhrée Mar 18 '14 at 18:25
  • In that case, `edit` then `git commit --amend -m "New commit message"` it will have to be I think. Thanks for bringing it to notice! I'll update the answer. – Ayush Chaudhary Mar 18 '14 at 18:27