git: Show index diff in commit message as comment

前端 未结 5 610
清歌不尽
清歌不尽 2021-01-30 05:10

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

# Please enter the commit message for your changes. Lines star         


        
相关标签:
5条回答
  • 2021-01-30 05:13

    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.

    0 讨论(0)
  • 2021-01-30 05:16

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

    0 讨论(0)
  • 2021-01-30 05:22

    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
    
    0 讨论(0)
  • 2021-01-30 05:24

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

    [alias]
    commit = commit -v
    
    0 讨论(0)
  • 2021-01-30 05:39

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

    git commit --verbose

    0 讨论(0)
提交回复
热议问题