git rebase and SHAs mentioned in log messages

211 views Asked by At

Sometimes the log message for a git commit will mention the SHA of other commits. This happens in the suggested log message for git revert and you also may add it by hand ('fixed bug introduced in abcd0123').

When rebasing, the SHAs of commits change. Is there a way I can automatically fix up the log messages to have the new SHAs? In general, this might not be possible in 100% of cases (a commit might disappear altogether, be squashed, etc). But 90% of the time a simple rebase leaves a one-to-one correspondence between the original and the rebased commits, so it should be possible to remap the SHAs in log messages.

I have tried git rebase --verbose, which I hoped would print something like

Applying: My log message here.
  old commit was 01234
  new commit is abcde

I could then use that to manually fiddle the log messages (with a further rebase... which would in turn change the SHAs... so it would be awkward, but nonetheless possible). But as far as I can tell --verbose doesn't print more information than the normal mode.

Is there some magic tool which will rebase and rewrite the log messages for me? In the absence of ponies, can I convince git rebase to print more information about old and new SHAs so I can do the job myself?

1

There are 1 answers

8
jthill On BEST ANSWER

Sometimes the log message for a git commit will mention the SHA of other commits[...]When rebasing, the SHAs of commits change. Is there a way I can automatically fix up the log messages to have the new SHAs?

You can use git patch-id to find commits that apply the same changes, the most brute-force method would be

git rev-list --no-merges --reflog --all \
| git diff-tree --stdin -p \
| git patch-id | sort > patch-ids

awk '{print $2,$1}' patch-ids | uniq -Df1

to find every set of commits in your repo that apply similar patches. A bit of git log --grep= applied to the results, and perhaps some restraint on your rev-list args, should get you where you want to go.

On GNU/anything replacing the uniq with

uniq -f1 --all-repeated=separate | awk '{print $1}' | awk '{$1=$1; print}' RS=

groups the commit ids for you. You could use join or some more awking to achieve the same effect elsewhere.

You can add git notes to a commit (or to any object), that might help if you're trying to distribute warnings about what happened.