Git: 将提交消息中的 index diff 显示为注释

git commit打开时,消息编辑器会显示一个简短的状态,如下所示:

# 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? 我知道这可能是一个很长的差异,但仍然. . 如此有用。

18110 次浏览

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

git commit --verbose

I've put the following lines in . git/hook/ready-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

这样,您不仅可以注释掉差异,而且还可以添加更多的信息(就像 stat选项所做的那样)。

编辑: 此外,Git 提交——冗长不包括提交消息的差异,这种方式没有 # s。

没有足够的声誉来回复 Alan 的回答,但是对于 Idan 和其他人来说,我只是尝试了一下,提交消息中的差异行没有被明确地注释掉。但是,它们仍然没有出现在最终提交消息中,谢天谢地。

$ git commit --verbose

在我的编辑里:

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!

(请注意在差异行之前缺少 #)

然后是实际的提交消息:

$ 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.

显然,git show仍然会显示差异,但这是因为它总是为提交。 :)

如果希望在提交时始终看到 diff,可以在 ~/.gitconfig文件中添加以下内容:

[alias]
commit = commit -v

确保 always存在这种行为的最简单方法是将这一部分添加到 git config文件:

[commit]
verbose = true

您可能需要将编辑器配置为实际以 diff 模式显示(用于语法突显)。我使用 Notepad2作为 Windows 记事本的替代品,而 -s diff适当地设置了配色方案(红色代表删除的行,等等) :

[core]
editor = C:/Windows/system32/notepad.exe -s diff