Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Change unpushed previous git commit message

Tags:

git

How to change a previous unpushed git commit message?

UPDATE: question dramatically reduced to suit for the answer. To see the full content. see the history.

So what's the correct way of doing it? Thx.

like image 824
xpt Avatar asked Dec 03 '22 21:12

xpt


1 Answers

To change the most recent commit's message, running:

git commit -m "corrected message" --amend

would be sufficient.

However, if you need to change the message of commit prior to the most recent, you would instead use an interactive rebase. This example would allow you to change the message of the commit just prior to the most recent. Adjust the ~2 as appropriate to go back further.

git rebase -i HEAD~2
<editor will open>
<change "pick" to "reword" for whichever commit(s) you wish to alter the message of>
<close/save the editor>
<for each commit you changed "pick" to "reword", git will present an editor with the existing commit message>
<update the commit message as desired and close/save the editor>

That's it. After git has stopped at each commit you switched from "pick" to "reword", you'll be back to your HEAD commit, with updated comments. Note that, starting from the first commit which had its comment altered, that commit and all subsequent commits will have a new hash. Since you indicated the commit had not been pushed, this should not cause an issue for anyone else using the repo.

like image 74
Ryan LaNeve Avatar answered Dec 07 '22 21:12

Ryan LaNeve