When working on a merge/pull request and want your Git commit history to look nice afterwards, you often need to rename commit messages. For larger merge/pull requests you might need to rename a bunch of them.
The git rebase interactive reword command comes close, but it only allows you to change commit messages one by one in sequence, instead of changing the messages in an editor in one go.
Below is the most practical way of rewriting the commit messages I have found. It uses git interactive rebase, vscode and multiple cursors.
Note that the method below doesn't work for multiline commit messages!
-
Open up interactive rebase in an editor of choice:
GIT_SEQUENCE_EDITOR=code git rebase --interactive $(git merge-base HEAD origin/master)git rebase --interactivesee git-scm documentationGIT_SEQUENCE_EDITOR=codeopens up Visual Studio Code to edit the interactive rebase sequence.$(git merge-base HEAD origin/master)rebase from the commit that you forked off from master. This is usually what you want to do when working on a pull-request/merge-request. -
In your editor there will be multiple
pickcommands.pick 1a2b3c My first commit pick 1a2b3c My Second commitMake sure each
pickcommand is followed by:exec git commit --amend --message "{new commit message}"In this example we will change
My first commitintoThis is my first commit.So that you get:
pick 1a2b3c My first commit exec git commit --amend --message "This is my first commit" pick 1a2b3c My Second commit exec git commit --amend --message "This is my second commit"In Visual Studio Code you can do this using multple cursors
-
Exit the editor. The rebase will start.