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


109

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
#

我如何调整git以显示要提交的差异?我知道这可能会比较长,但仍然很有用。


您要做什么,需要提交消息中列出的更改?您可能对特定工具有误解,我们也许可以帮助您解决实际问题。
Mark Rushakoff 2011年

1
会为您提供一些您无法从“ git log -p”获得的信息吗?
杰德·施耐德

12
@Mark:OP希望对差异进行注释。它是默认已注释掉提示的更详细的版本。@Jed:OP希望在提交消息模板中提供此信息。是的,git diff --cached会生成它,但是如果每次都需要运行单独的命令,为什么呢?
卡斯卡贝尔2011年

1
github.com/tpope/vim-fugitive/issues/149跟随这个逃犯,以防万一。如果不是,那应该是。
lkraav

Answers:


149

--verbose(或-v)标志git commit将显示要提交的内容的差异:

git commit --verbose


3
似乎差异未注释掉,默认情况下有任何方法使其成为注释吗?
伊丹·K

25
diff消息不需要注释掉,Idan;git知道忽略它们,就像它们是注释一样。
布兰登·罗兹

@BrandonRhodes如何告诉git bit ti忽略差异?我有一个脚本,可以以diff格式生成一些附加行,但是它从第一行开始被截断了
Dennis C

2
@IdanK,对于在提交消息模板中未注释掉差异行的一个好处是,vim代码的颜色对添加的差异内容有效(如果您已为vim安装了git文件类型)。如果这些差异线被注释掉,则此着色将不起作用。
克里斯托弗·

1
忽略我先前的评论;罪魁祸首是EditorConflig插件与“请勿触摸上方的行”这一行搞混了。
Daniel Liuzzi

31

没有足够的声誉来发布对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仍然会显示差异,但这是因为它始终对提交起作用。:)


2
在新版本的git(我有2.3)中,diff带有以下行前缀:# ------------------------ >8 -------------------------我认为git会自动删除其后出现的所有内容。
JanWarchoł2015年

从至少2.4.1开始,我看到:#------------------------> 8 ------------ ------------#请勿触摸上方的线。#以下所有内容将被删除。所以这证实了您的想法。
安东尼·潘诺佐

@AnthonyPanozzo这似乎在2.5.3上被破坏了。尽管标记为“请勿触摸上方的线”,但完整的差异仍然会在提交消息中出现。
Daniel Liuzzi

在2.8.2中为我工作
Anthony Panozzo

3
要从命令行设置:git config --global commit.verbose true
Taylor Edmiston

10

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

[commit]
  verbose = true

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

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

9

我将以下行放在.git / hooks / prepare-commit-msg中,以获取差异注释:

#!/bin/bash

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

这样,您不仅可以注释掉差异,还可以添加更多信息(如stat选项一样)。

编辑:同样git commit --verbose不包括对提交消息的差异,这种方式将在没有#s的情况下进行。


4
取而代之的评论,你可以使用# ------------------------ >8 ------------------------你可以找到更多git-scm.com/docs/git-commit#git-commit-scissors
AB

8

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

[alias]
commit = commit -v

但是,您可以-您正在使用什么版本的git?这对我有用w /版本2.0.0
生锈的

2
您的回答看起来很有希望,但对我也不起作用。我尝试过,git config --global alias.commit 'commit -v'并且按照您的建议添加了别名-只是自动进行的。我创建了另一个别名cv,该别名的作用与预期的一样。我的git版本是Ubuntu 15.10中打包的2.5.0。
DanielBöhmer16年

6
要从命令行设置:git config --global commit.verbose true
Taylor Edmiston
By using our site, you acknowledge that you have read and understand our Cookie Policy and Privacy Policy.
Licensed under cc by-sa 3.0 with attribution required.