Ronald
Ronald

Reputation: 2917

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?

Upvotes: 3

Views: 9791

Answers (3)

Julian
Julian

Reputation: 36700

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

Upvotes: 5

kowsky
kowsky

Reputation: 14429

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.

Upvotes: 2

Andreas Wederbrand
Andreas Wederbrand

Reputation: 39951

It's considered best practice to not alter what has already been pushed.

However, you can edit any commit with git rebase -i and choose r (reword) for the commit you'd like to change.

You need to push it with push -f (forced push) if you've pushed it before.

You will alter the entire tree from that commit and forward so all other that have cloned the repository will need to do a git pull to get it right. If they have changes based on the current tree you will make it hard for them so best thing is to make everyone push their changes, "freeze" the repository and then have them pull again after your push.

Upvotes: 1

Related Questions