Chris Maes
Chris Maes

Reputation: 37712

git show difference between commit messages

suppose I have two commits that do exactly the same, but whose messages differ, how can I see this in git?

How to produce this; suppose I am on master, on any commit;

git checkout -b test
git commit --amend
// now edit the commit message
git diff master

this shows an empty output. The only way I found to see this difference in commit messages is:

git show --stat master > m
git show --stat > t
diff m t

which produces output like this (I did modify the git log output format a little):

1c1
< 65fb678 - (HEAD, test) add bcdef (Fri, 8 Jan 2016 11:23:51 +0100) <Chris Maes>
---
> 7f9c3ee - (master) add bcd (Wed, 6 Jan 2016 11:28:10 +0100) <Chris Maes>

is there any git command that allows to just see the difference in commit messages (with or without the normal git output)?

NOTE my question ressembles this question, but I was looking for a git command that would allow this.

Upvotes: 2

Views: 1263

Answers (2)

sls
sls

Reputation: 294

Starting with Git 2.19.0 (~Sep 2018), there's a command for comparing two commit ranges: git range-diff.

To compare two individual commits referred to by <rev1> and <rev2>, they need to be expressed in terms of single-commit ranges like this:

git range-diff <rev1>^! <rev2>^!

Example

echo "Some file content" > file.txt
git add file.txt
git commit -m "Add a file" -m "Just an uninteresting file." -m "It has 1 line"
git tag old
git commit --amend --edit
# ... Make some modifications to the commit message ...
git tag new

git range-diff old^! new^!

Output:

1:  c225627 ! 1:  99b9ca3 Add a file
    @@ Commit message
         
    -    It has 1 line
    +    It has 1 line and 3 words.
     
      ## file.txt (new) ##
     @@

Caveats / Limitations

  • The content of the compared commits needs to be sufficiently similar for them to be recognized by range-diff as a matching pair, rather than as a complete rewrite (i.e. removal of one commit + addition of another unrelated commit). This should be fine for the scenario described in the question ("two commits that do exactly the same, but whose messages differ").
  • Does not work with merge-commits
  • This is a porcelain command with output meant for humans. The format is subject to change in the future.
  • (rare edge case) The single-commit range syntax <rev>^! does not work for parent-less root commits

Upvotes: 1

Amedee Van Gasse
Amedee Van Gasse

Reputation: 7634

This does the trick for me:

diff -w <(git rev-list --max-count=1 --format=%B SHA1) <(git rev-list --max-count=1 --format=%B SHA2)
  • -w ignores whitespace differences.
  • The <( ... ) syntax creates a temporary named pipe which makes the stdout of git show commands look and behave like a file, allowing diff to operate on the expected type of input.
  • --format=%B shows the raw message header+body of a commit message

You could replace diff -w with wdiff to have a word per word comparison.

EDIT

If you really want a git command, add this git alias to ~/.gitconfig:

[alias]
        msgdiff = "!bash -c '[ $# = 2 ] && diff -w <(git rev-list --max-count=1 --format=%B \"$1\") <(git rev-list --max-count=1 --format=%B \"$2\")' -"

Then you can do

git msgdiff SHA1 SHA2

Upvotes: 3

Related Questions