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.

4
  • 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. Jan 20, 2011 at 17:06
  • 1
    does that provide you some information you don't get with 'git log -p'? Jan 20, 2011 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, 2011 at 18:55
  • 1
    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, 2012 at 11:36

5 Answers 5

173

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

git commit --verbose

7
  • 4
    it seems that the diff isn't commented out, any way to make it a comment by default?
    – Idan K
    Feb 24, 2011 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. Mar 25, 2012 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 8, 2013 at 0: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. Aug 28, 2013 at 1:59
  • 1
    Disregard my previous comment; the culprit was EditorConflig plugin messing with the "Do not touch the line above" line. Jun 8, 2016 at 14:11
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 <[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. :)

7
  • 4
    In new versions of git (I have 2.3) the diff is prefixed with the following line: # ------------------------ >8 ------------------------ - i presume that git automatically removes everything that appears after it. Mar 5, 2015 at 10:25
  • 1
    As of at least 2.4.1, I see: # ------------------------ >8 ------------------------ # Do not touch the line above. # Everything below will be removed. so this confirms what you were thinking. May 23, 2015 at 14:19
  • @AnthonyPanozzo This seems to be broken on 2.5.3. Despite the "Do not touch the line above" marker, the full diff ends up in the commit message anyway. Jun 8, 2016 at 14:02
  • Works for me in 2.8.2 Jun 8, 2016 at 14:06
  • 11
    To set this from the command line: git config --global commit.verbose true Dec 2, 2017 at 23:38
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
12

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.

1
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
3
  • You can, though -- what version of git are you on? This works for me w/ version 2.0.0
    – rusty
    Nov 12, 2015 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. Feb 8, 2016 at 15:54
  • 6
    To set this from the command line: git config --global commit.verbose true Dec 2, 2017 at 23:40

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.