128

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.

Assaf Lavie
  • 73,079
  • 34
  • 148
  • 203
  • What is it you're trying to do that you need the changes listed in the commit message? You might just have a misunderstanding with a particular tool, and we might be able to help you work around the actual problem. – Mark Rushakoff Jan 20 '11 at 17:06
  • 1
    does that provide you some information you don't get with 'git log -p'? – Jed Schneider Jan 20 '11 at 17:07
  • 12
    @Mark: The OP wants the diff commented out. It's a more verbose version of the default commented-out hint. @Jed: The OP wants this information in the commit message template. Yes `git diff --cached` produces it, but why run a separate command if you want it every time? – Cascabel Jan 20 '11 at 18:55
  • 1
    https://github.com/tpope/vim-fugitive/issues/149 is following this for fugitive, in case you're wondering. If you aren't, you should be. – lkraav Feb 24 '12 at 11:36

5 Answers5

173

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

git commit --verbose

Hotschke
  • 9,402
  • 6
  • 46
  • 53
Alan Haggai Alavi
  • 72,802
  • 19
  • 102
  • 127
  • 4
    it seems that the diff isn't commented out, any way to make it a comment by default? – Idan K Feb 24 '11 at 15:34
  • 30
    The diff messages do not need to be commented out, Idan; git knows to ignore them as though they are comments. – Brandon Rhodes Mar 25 '12 at 11:21
  • @BrandonRhodes How to tell git bit ti ignore the diff ? I've a script to generate few addition lines in diff format, but it got truncated from the first line – Dennis C Apr 08 '13 at 00:58
  • 2
    @IdanK, a benefit to the diff lines not being commented out in the commit message template is that vim code colouring works on the diff content added (if you have the git file type additions installed for vim). If these diff lines were commented out, this colouring wouldn't work. – Christopher Aug 28 '13 at 01:59
  • @BrandonRhodes Unfortunately the full diff is included in the commit message. Despite being preceded by `# ------------------------ >8 ------------------------` and `# Do not touch the line above.` markers, the full diff ends up in the commit message anyway. This is on **2.5.3.windows.1** by the way. – Daniel Liuzzi Jun 08 '16 at 14:00
  • 1
    Disregard my previous comment; the culprit was EditorConflig plugin messing with the "Do not touch the line above" line. – Daniel Liuzzi Jun 08 '16 at 14:11
  • git commit -av notes: # Do not modify or remove the line above. # Everything below it will be ignored. – neoexpert Aug 13 '23 at 10:37
35

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

tomjakubowski
  • 645
  • 1
  • 6
  • 14
17

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
Ryan Lundy
  • 204,559
  • 37
  • 180
  • 211
11

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.

Michael
  • 1,502
  • 19
  • 29
  • 4
    Instead of commenting you can use `# ------------------------ >8 ------------------------` you can find more https://git-scm.com/docs/git-commit#git-commit-scissors – A B May 10 '17 at 22:03
9

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

[alias]
commit = commit -v
rusty
  • 2,771
  • 1
  • 24
  • 23
  • You can, though -- what version of git are you on? This works for me w/ version 2.0.0 – rusty Nov 12 '15 at 15:24
  • 2
    Your answer looks promising but doesn't work for me either. I tried `git config --global alias.commit 'commit -v'` and the alias was added like you suggested--just automatically. I created another alias named `cv` that works like expected. My git version is 2.5.0 as packaged in Ubuntu 15.10. – Daniel Böhmer Feb 08 '16 at 15:54
  • 6
    To set this from the command line: `git config --global commit.verbose true` – Taylor D. Edmiston Dec 02 '17 at 23:40