I would like to modify the message of a particular commit, I know that with the following command I can modify the message of the last commit made:
git commit --amend -m "your new message"
But how can I modify the message of the penultimate commit made? It's possible?
You can use
git rebase
to go to an old commit. For example, if you have these commits:and you want to modify the
Commit 2
you would do the following:In the editor that will come out (the default) put
edit
,r
orreword
instead ofpick
in the line of the commit you want to modify.Something like that:
Make your changes (if any) and then make the new commit:
And after that:
To return to your
HEAD
previous.Caution:
This is not recommended in public repositories since if there are programmers who based their changes on subsequent commits, they will have a kind of ghost code and it will cause a lot of confusion. But in your case, if you're just going to change the message then there's not much of a problem. You can see those recommendations here.
What you have to do is an
rebase
interactive (-i
) using the same commandreword
.First run
git rebase -i <hash-del-commit>~1
to ask to makerebase
the current branch interactive from the previous commit (~1
) that you want to modify. That will fire you$EDITOR
with a list of all the commits that exist from that to the top of your branch, indicating thepick
, hash and message of each one. Something like:Change
pick
tor
orreword
in the line of the commit to edit. Something like that:Save the file and exit the editor, and it's
git
going to go back to thatcommit
and fire the$EDITOR
again, but this time with the commit message there. Something like:Edit the post to make it the way you want, save the file, exit the editor, and
git
apply all the missing commits so your branch looks the same, except for having the updated post.Do not forget that you are modifying the history of your repository , so if you had published the commit to which you are editing the message or any of its successors, you can generate conflicts with the rest of your team.
This implies rewriting history. You must take into account that it is CONTRAINDICATED to rewrite the history of commits that are already in a
remote
.That said, you can rewrite the history and therefore change the message of a commit using the command
git-rebase
with the following syntax:Then git opens an editor with content similar to the following:
In this editor you change the word pick to the word reword
Git will then open an editor where you can capture the new message of your commit.
Once you close the editor, git will rewrite post-reword commits and update your current branch reference
Cheers