git: Show index diff in commit message as comment

Git

Git Problem Overview


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.

Git Solutions


Solution 1 - Git

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

git commit --verbose

Solution 2 - Git

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 <tom@crystae.net>
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 - Git

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

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

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

[alias]
commit = commit -v

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionAssaf LavieView Question on Stackoverflow
Solution 1 - GitAlan Haggai AlaviView Answer on Stackoverflow
Solution 2 - GittomjakubowskiView Answer on Stackoverflow
Solution 3 - GitRyan LundyView Answer on Stackoverflow
Solution 4 - GitMichaelView Answer on Stackoverflow
Solution 5 - GitrustyView Answer on Stackoverflow