Search code examples
gitgit-diff

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.


Solution

  • 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