Last Updated: August 15, 2019
·
2.182K
· fperezp

Modify your commit messages

Hi guys!

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!

5 Responses
Add your response

Awesome. Thanks.

over 1 year ago ·

Good one, thanks.

over 1 year ago ·

An extra tip: if you only want to change the commit message instead of editing the commit itself you can use reword instead of edit and git will take care of everything for you :)

over 1 year ago ·

Git it self is trying to help you do things easier, it adds a comment below the hashes telling you the available commands, I usually just put an r for reword
Here's the available options

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,

Also a tip, reordering commit lines would reorder them in the new history you're writing, and deleting a line would drop that commit from the new history.

over 1 year ago ·

Thanks, pretty consice and clear.
Just to clarify your last warning, if any other user pulled your commits before you changed the message, when they try to pull again it'll all fail horribly.
So again: don't use this if somebody has pulled your repo!

over 1 year ago ·