Sometimes, checking my git log, I realize that I have a typo in one of my commits messages
or just I forgot to add the issue number, but the affected commit is not the last one, so «commit
--amend» doesn't work. To solve it, I use «git rebase --interactive». This git
tool is much powerful than this, but this is one of its usage.
Well, all we have to do is get the previous commit hash of the affected commit. Having it, we
just have to execute this:
$ git rebase --interactive <commit_hash>
Done it, we will enter in our predefined git editor, getting something similar to this:
pick 1c3f102 Message 1
pick b74bea3 Message 2
pick 1826d6b Message 3
pick 09e62fb Message 4
# Rebase cfaa885..09e62fb onto cfaa885
Now, we have to replace «pick» with «edit» in the commit lines we want to edit the commit
message. Save and exit. We'll be back to the prompt. Now we are placed at the commit we want to
edit, so we can amend it. To edit the message we will execute:
git commit --amend
Save and exit. Now we have to continue with the rebase op. To do it, just execute:
git rebase --continue.
We have to repeat these steps for each commit line you edit in the «git rebase --interactive» action.
Once we've finished, we have modified our git log messages. Take care that this kind of modifications
should be done before any push. If we have made a push before modifying our git history, we will
have to force the push. This is a critical op if we are not the only one who is working with the
branch we are working with, so please, be carefull!
That's all. Comments are welcome!