Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to edit commit message of a pushed git commit

the commit I would like to change its message is already pushed and it is in the middle of 5 commits. Is there any way to edit the commit message? What happend after editing, when somebody has already pulled the commit?

like image 502
Ronald Avatar asked Mar 09 '23 15:03

Ronald


2 Answers

As this is tagged with tortoisegit, I will show you how you could achieve this in TortoiseGit

  1. Go to the commit log
  2. Select the commit below the commit to edit and select "Rebase ... onto this" enter image description here
  3. The rebase dialog opens. First select "force rebase" as there isn't a really need for a rebase from Git's perspective enter image description here
  4. Then select the commit and choose edit enter image description here
  5. Press the "Start rebase" button below
  6. Now you could edit your message and press "Amend" afterwards enter image description here
  7. After this push it. If the commit was already pushed, then you need a force push. Check the "known changes" in the push dialog for this.

Update: updated this answer to start from the log. It isn't easier, but it's better and will not result in conflicts

like image 58
Julian Avatar answered Mar 14 '23 06:03

Julian


It is generally not advisable to alter commits that are already publicly availabe. As you realized yourself, editing such a commit when someone else is already working on top of it may lead to conflicts when the other person tries to publish their work.

Having said that, git rebase -i HEAD~5 will allow you to interactively rebase your last five commits. It will open a ToDo file in your editor that allows yout to rewrite your history as needed. Among others, there will be a reword option, allowing you to change the commit message for a particular commit. You just have to change the pick in front of said commit to reword and save the file, and git will promt you for the new commit message.

Be aware, hoever, that you will create an entirely new commit, and if you want to publish it, you will have to push with the --force option.

like image 39
kowsky Avatar answered Mar 14 '23 07:03

kowsky