Skip to content

Instantly share code, notes, and snippets.

@bobvanderlinden
Last active July 20, 2022 09:59
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save bobvanderlinden/d77a3314d41cdd86975ae77b08f0af27 to your computer and use it in GitHub Desktop.
Save bobvanderlinden/d77a3314d41cdd86975ae77b08f0af27 to your computer and use it in GitHub Desktop.
Rename Git commits in batch using interactive rebase

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!

  1. Open up interactive rebase in an editor of choice:

    GIT_SEQUENCE_EDITOR=code git rebase --interactive $(git merge-base HEAD origin/master)
    

    git rebase --interactive see git-scm documentation

    GIT_SEQUENCE_EDITOR=code opens 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.

  2. In your editor there will be multiple pick commands.

    pick 1a2b3c My first commit
    pick 1a2b3c My Second commit
    

    Make sure each pick command is followed by:

    exec git commit --amend --message "{new commit message}"
    

    In this example we will change My first commit into This 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

  3. Exit the editor. The rebase will start.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment