Search code examples
gitgit-commitgit-rebase

Modify commit message of past commit in git


All I need to do is modify the commit message of some past commits in my branch locally, then push those modifications up to the server. I don't need to actually change any files in the past commits, and the past commits are not the most recent, they're from 2-3 commits ago.

Most answers I've seen deal with either modifying the files of past commits, or else just modifying the most recent commit message. Neither is quite what I'm looking for.

I'm hoping this is a quick and easy one for all you git experts :-)


Solution

  • This generally can't be done in a completely safe manner regardless of context, but it can be done.

    The easiest way to do it is to do an interactive rebase ( potentially preserving merges ) and marking that you'd like to edit the commit messages, editing them and then force pushing the branch.

    This will alter the hashes of all the commits starting from the first one you modify to the tip of the branch. If this branch is merged to master already, this is probably a bad idea unless you check with everyone else involved with the project.

    Before you start this endeavor I strongly suggest you consider whether anyone else has consumed your branch yet, are there any merges you have to preserve, etc.