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?
-
You will create diverging histories because if you change the commit message you will get new hashes identifying that comment and everything that follows. I would first ensure you **really** must change this commit message or if you can live with the error. – Lasse V. Karlsen Mar 13 '17 at 09:28
-
Possible duplicate of [Git: Change already pushed commit message using git rebase](http://stackoverflow.com/questions/42252725/git-change-already-pushed-commit-message-using-git-rebase) – Sajib Khan Mar 13 '17 at 09:37
3 Answers
As this is tagged with tortoisegit, I will show you how you could achieve this in TortoiseGit
- Go to the commit log
- Select the commit below the commit to edit and select "Rebase ... onto this"
- The rebase dialog opens. First select "force rebase" as there isn't a really need for a rebase from Git's perspective
- Then select the commit and choose edit
- Press the "Start rebase" button below
- Now you could edit your message and press "Amend" afterwards
- 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

- 33,915
- 22
- 119
- 174
-
1
-
can this be done, even if the bad commit is already in origin and other people have already pulled? How its behavior, when other people pull the edited commit? – Ronald Mar 21 '17 at 08:41
-
Yes as you could do everything with git, but others will have a bad time. Not recommended – Julian Mar 21 '17 at 21:55
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.

- 12,647
- 2
- 28
- 41
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.

- 38,065
- 11
- 68
- 78