8

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...

leppie
  • 112,162
  • 17
  • 191
  • 293
Totor
  • 888
  • 1
  • 7
  • 18
  • 1
    Do you have any merges in your history? `git rebase` does not include merge commits by default. Even with the `--preserve-merges` option, when it attempts to re-merge, it will ignore your previous merge resolution. – Joseph K. Strauss Dec 22 '14 at 16:52
  • @JosephK.Strauss Thanks, do you have a workaround? – Totor Dec 29 '14 at 13:04
  • I believe `git filter-branch` will do what you want, if you use `msg-filter`, but you will have to filter the entire branch, but structure the command to only change the message for the one specified commit. I may leave a better answer late today. – Joseph K. Strauss Dec 29 '14 at 13:10

1 Answers1

16

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/'
Joseph K. Strauss
  • 4,273
  • 1
  • 21
  • 36