git: changing an old commit message without creati

2020-05-29 13:08发布

问题:

I want to change a pretty old commit message using:

git rebase -i sha1-of-commit^

That's nice, I do git commit --amend and edit the message, but things get bad when I do:

git rebase --continue

I encounter multiple conflicts but don't understand why as the whole conflict resolution has obviously already been done in the past, and git should just move forward until all commits are rebased.

How can I finish the rebase quickly without having to handle these old conflicts? I just want to change a simple (and old) commit message after all...

回答1:

Make a small script in your /bin directory (or any directory in your path) named git-reword-commit. (You can name it whatever you want as long as the name starts with git-. This will work regardless of whether there are merge commits or not.

#! /bin/bash
REV=$1
MESSAGE=$2
FILTER="test $(echo '$GIT_COMMIT') = $(git rev-parse $REV) && echo $MESSAGE || cat"
git filter-branch --msg-filter "$FILTER" -- --all

To use, execute

git reword-commit <commit> 'new message'

Warning: This will rewrite many commits, so the same issues that apply to rebase, apply here as well, i.e., you will need to force when you push, and other users will have to know about this.

Git puts your original refs (from before the filter-branch) in .git/refs/original. You can use the following aliases to undo/confirm any filter-branch command.

git config alias.undo-filter-branch '! DIR=$(git rev-parse --git-dir); cp -r $DIR/refs/original/refs/ .git/; rm -r $DIR/refs/original/'
git config alias.confirm-filter-branch '! DIR=$(git rev-parse --git-dir); rm -r $DIR/refs/original/'