git: Show index diff in commit message as comment

When git commit open the message editor is shows a brief status, something like this:

# Please enter the commit message for your changes. Lines starting
# with '#' will be ignored, and an empty message aborts the commit.
# On branch master
# Your branch is ahead of 'origin/master' by 26 commits.
#
# Changes to be committed:
#   (use "git reset HEAD <file>..." to unstage)
#
#   modified:   Showcase/src/com/gigantt/BorderArea.mxml
#   modified:   Showcase/src/com/gigantt/Client.mxml
#   modified:   Showcase/src/com/gigantt/GraphItem.mxml
#

How can I tweak git to show also the diff to be committed? I'm aware that it may be a long diff, but still.. so useful.


Solution 1:

The --verbose (or -v) flag for git commit will display the diff of what would be committed:

git commit --verbose

Solution 2:

Not enough reputation to post a reply to Alan's answer, but for Idan and anyone else I just tried it out and the diff lines in the commit message aren't explicitly commented out. However, they still don't show up in the final commit message, thank goodness.

$ git commit --verbose

In my editor:

Feeling a bit pessimistic now.

# Please enter the commit message for your changes. Lines starting
# with '#' will be ignored, and an empty message aborts the commit.
# On branch master
# Changes to be committed:
#   (use "git reset HEAD <file>..." to unstage)
#
#   modified:   README
#
diff --git a/README b/README
index af5626b..c62237e 100644
--- a/README
+++ b/README
@@ -1 +1 @@
-Hello, world!
+Goodbye, world!

(note the lack of # preceding the diff lines)

And then the actual commit message:

$ git log -n 1
commit ad21a2655ef6d8173c2df08dc9893055b26bc068
Author: Tom Jakubowski <[email protected]>
Date:   Thu Oct 27 19:12:54 2011 -0700

    Feeling a bit pessimistic now.

Obviously, git show will still show the diff, but that's because it always does for commits. :)

Solution 3:

The simplest way to make sure this behavior is always present is to add this section to your git config file:

[commit]
  verbose = true

You may need to configure your editor to actually display in diff mode (for syntax highlighting). I use Notepad2 as a Windows Notepad replacement, and -s diff sets the color scheme appropriately (red for deleted lines, etc.):

[core]
  editor = C:/Windows/system32/notepad.exe -s diff

Solution 4:

I've put the following lines in .git/hooks/prepare-commit-msg to get a commented out diff:

#!/bin/bash

if [ "$2" == "" ] ; then
    git diff --staged -p --stat 2> /dev/null | awk '{ printf "#"; print}' >> "$1"  2>/dev/null
fi

This way you can not only comment out the diff, but also add more info (like the stat option does).

Edit: Also git commit --verbose does not include the diff to the commit message this way would do without the #s.

Solution 5:

If you want to always see the diff when you commit, you can add the following to your ~/.gitconfig file:

[alias]
commit = commit -v