git:在提交消息中显示索引差异作为注释

Ass*_*vie 107 git

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
#
Run Code Online (Sandbox Code Playgroud)

我如何调整git以显示要提交的差异?我知道它可能是一个很长的差异,但仍然......非常有用.

Ala*_*avi 145

--verbose(或-v)标志git commit将显示这将是犯下的DIFF:

git commit --verbose

  • Idan,差异消息不需要注释掉; git知道忽略它们就好像它们是评论一样. (25认同)
  • 似乎差异没有被注释掉,默认情况下任何方式使它成为评论? (3认同)
  • @IdanK,在提交消息模板中未被注释掉的diff行的好处是vim代码着色适用于添加的diff内容(如果你为vim安装了git文件类型添加).如果这些差异线被注释掉,这种着色将不起作用. (2认同)

tom*_*ski 31

没有足够的声誉来回复Alan的答案,但对于Idan和其他任何人我只是尝试了它并且提交消息中的diff行没有明确注释掉.但是,他们仍然没有出现在最后的提交消息中,谢天谢地.

$ 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!
Run Code Online (Sandbox Code Playgroud)

(注意缺少#前面的差异线)

然后是实际的提交消息:

$ 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.
Run Code Online (Sandbox Code Playgroud)

显然,git show仍会显示差异,但那是因为它总是用于提交.:)

  • 要从命令行设置它:`git config --global commit.verbose true` (3认同)
  • 在新版本的git(我有2.3)中,diff的前缀是以下行:`#------------------------> 8 --- ---------------------` - 我认为git会自动删除它后面出现的所有内容. (2认同)

Mic*_*ael 9

我在.git/hooks/prepare-commit-msg中添加了以下行来获取注释掉的diff:

#!/bin/bash

if [ "$2" == "" ] ; then
    git diff --staged -p --stat 2> /dev/null | awk '{ printf "#"; print}' >> "$1"  2>/dev/null
fi
Run Code Online (Sandbox Code Playgroud)

这样你不仅可以注释掉diff,还可以添加更多信息(比如stat选项).

编辑:另外git commit --verbose不包含提交消息的差异,这样做没有#s.

  • 而不是评论你可以使用`#------------------------> 8 ---------------- --------`你可以找到更多https://git-scm.com/docs/git-commit#git-commit-scissors (4认同)

Rya*_*ndy 9

确保始终存在此行为的最简单方法是将此部分添加到您的git config文件中:

[commit]
  verbose = true
Run Code Online (Sandbox Code Playgroud)

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

[core]
  editor = C:/Windows/system32/notepad.exe -s diff
Run Code Online (Sandbox Code Playgroud)


rus*_*sty 8

如果要在提交时始终看到差异,可以将以下内容添加到~/.gitconfig文件中:

[alias]
commit = commit -v
Run Code Online (Sandbox Code Playgroud)

  • 要从命令行设置它:`git config --global commit.verbose true` (6认同)
  • 你的答案看起来很有希望,但也不适合我.我尝试了`git config --global alias.commit'commit -v'`并且添加了别名,就像你建议的那样 - 只是自动添加.我创建了另一个名为`cv`的别名,它的工作方式与预期的一样.我的git版本是在Ubuntu 15.10中打包的2.5.0. (2认同)