6

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.

xpt
  • 20,363
  • 37
  • 127
  • 216
  • Possible duplicate of [How to modify existing, unpushed commits?](https://stackoverflow.com/questions/179123/how-to-modify-existing-unpushed-commits) – phd Jul 29 '17 at 22:21
  • Oh, true, it's a duplicate, but I do prefer Ryan LaNeve's answer better because it is clearer, especially on what exactly to do after `git rebase`, which wasn't covered at all in that answer. Moreover, that question itself wasn't clear and focusing on the ***previous*** git commit either. I.e., both the question and the answer are somewhat flawed. – xpt Jul 30 '17 at 01:27

1 Answers1

18

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.

Ryan LaNeve
  • 1,214
  • 1
  • 10
  • 15
  • @xpt , if you want to edit the first commit on your branch, then use `git commit --amend -m "corrected message"`. If your commit is not the first one, use interactive rebase to reword the desired commit as mentioned in above answer. – Rishikesh Darandale Jul 29 '17 at 16:33
  • Sorry about that @xpt - I screwed up the command; it should not have the word "commit" in it. Please try again using the updated command. – Ryan LaNeve Jul 29 '17 at 17:15
  • Oh, now I get you -- your answer is the _correct_ way, provided that I _had done nothing_. However, before I do that, I want to clear myself out of the mess I've made so far. I've tried your first suggestion twice, thus leaving 2 different detached commit behind in my git now. Would you update your answer to include that part please -- I searched for "_git detached head remove_", but the answers I found are not suitable for this particular case. Thanks a lot! – xpt Jul 29 '17 at 17:47
  • I'm not going to update the answer w/ regard to undoing the various things you tried, as that's not really applicable to your original question. I'd also recommend you edit your question to get back to the original (once you're totally done). You can use `git reflog` to see the history of actions you've taken. Find the commit you had before you started trying the various commands, and then use `git reset --hard ` to reset your branch back to that point. Then, you can use the interactive rebase (this answer) to do what you wanted to do in the first place. – Ryan LaNeve Jul 29 '17 at 20:55
  • OK, I'll strip it to the minimum, even it started with the situation of git leaving 1 detached commit behind at the very beginning. Will do in 4 hours and post back. – xpt Jul 29 '17 at 21:49
  • Brilliant, simple and easily understandable solution to the original question. No need to read the above comments. – Martin Bamford Dec 11 '18 at 09:20