Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

git: Show index diff in commit message as comment

Tags:

git

People also ask

How can I see the diff of a commit?

To see the diff for a particular COMMIT hash, where COMMIT is the hash of the commit: git diff COMMIT~ COMMIT will show you the difference between that COMMIT 's ancestor and the COMMIT .

How do I show last changes made with commit message?

If you want to see what's happened recently in your project, you can use git log . This command will output a list of the latest commits in chronological order, with the latest commit first.

How do I see changes not staged for commit?

If you just want to see the diff without committing, use git diff to see unstaged changes, git diff --cached to see changes staged for commit, or git diff HEAD to see both staged and unstaged changes in your working tree.


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

git commit --verbose


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. :)


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

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.