There are an enormous number of good web guides on how to amend old commit messages with git rebase. In my case this is essential as the hooks won't let me push remotely without changing the message. However, the guides I've seen seem to assume that rebasing can be done straightforwardly without needing to resolve conflicts. However, I find myself in the a strange catch-22 as follows:
- I am able to reword the commit message in the git editor and save it using git rebase --interactive
- The rebase fails because there are merge conflicts.
- I am asked to resolve the conflicts and I do.
- The rebase succeeds.
- Obviously the wrong commit message is not corrected retrospectively so I need to correct it.
- I rebase again to change the commit message.
- The second rebase revisits the problematic commit with the merge conflicts and so I've made no progress.
Basically it seems to be like this: I can't push remotely without changing the commit message. I can't change the commit message without rebasing to that commit. I can't rebase to that commit without solving the merge conflicts first. I must now rebase to that commit again to change the commit message. But I've gone into reverse because I've now reintroduced the merge conflicts that I had previously solved.
I tried rewording the commit message, saving the message and fixing the merge conflicts.
Actually, that should not be the case: when you successfully complete a rebase, even if it involves resolving conflicts, the changes you have made (including amending commit messages) are already part of the new rebased commit history. So, a second rebase should not be necessary just to amend commit messages.
Start with an interactive rebase using
git rebase -i $(git merge-base main HEAD)(assumingmainis the branch you are rebasing onto). This will start the rebase at the merge base of your branch andmain. This is the point at which your branch diverged frommain, and is the point at which you want to start the rebase.In the interactive rebase list, choose to
rewordthe desired commits. Proceed through the rebase. If a conflict arises in any of the commits, resolve the conflict, then continue the rebase. Once you reach the commit(s) you wish to reword, Git will pause and allow you to change the commit message(s).If conflicts occur during the rebase, Git pauses and allows you to resolve them. After resolving each conflict, you would typically
git addthe resolved files and thengit rebase --continueto proceed with the rebase process.The key point is that once you resolve a conflict and continue, the resolved state of that conflict is part of the new commit being formed by the rebase.
After resolving conflicts and rewording commits, once you complete the rebase process (
git rebase --continueuntil it finishes), your branch's commit history has been rewritten. It now includes the amended commit messages and any conflict resolutions you performed.There is no need for a second rebase just to change commit messages, as these changes are already part of the rewritten history.
If you are satisfied with the changes (amended messages and conflict resolutions), you can push these changes to the remote repository. If you have rewritten history, this will likely require a force push (using
git push --force-with-leaseor, Git 2.30+;git push --force-if-includes).