git:将提交消息中的索引差异显示为注释
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/4750148/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
git: Show index diff in commit message as comment
提问by Assaf Lavie
When git commit
open the message editor is shows a brief status, something like this:
当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? I'm aware that it may be a long diff, but still.. so useful.
如何调整 git 以显示要提交的差异?我知道这可能是一个很长的差异,但仍然......非常有用。
回答by Alan Haggai Alavi
The --verbose
(or -v
) flag for git commit
will display the diff of what would be committed:
该--verbose
(或-v
)标志git commit
将显示这将是犯下的DIFF:
git commit --verbose
git commit --verbose
回答by tomjakubowski
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.
没有足够的声誉来回复 Alan 的答案,但对于 Idan 和其他任何人,我只是尝试了一下,并且没有明确注释掉提交消息中的差异行。然而,他们仍然没有出现在最终的提交信息中,谢天谢地。
$ git commit --verbose
$ 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. :)
显然,git show
仍然会显示差异,但那是因为它始终适用于提交。:)
回答by Ryan Lundy
The simplest way to make sure this behavior is alwayspresent is to add this section to your git config
file:
确保此行为始终存在的最简单方法是将此部分添加到您的git config
文件中:
[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.):
您可能需要将编辑器配置为以差异模式实际显示(用于语法突出显示)。我使用 Notepad2 作为 Windows 记事本替代品,并-s diff
适当地设置配色方案(红色表示已删除的行等):
[core]
editor = C:/Windows/system32/notepad.exe -s diff
回答by Michael
I've put the following lines in .git/hooks/prepare-commit-msgto get a commented out diff:
我将以下几行放在.git/hooks/prepare-commit-msg 中以获得注释掉的差异:
#!/bin/bash
if [ "" == "" ] ; then
git diff --staged -p --stat 2> /dev/null | awk '{ printf "#"; print}' >> "" 2>/dev/null
fi
This way you can not only comment out the diff, but also add more info (like the statoption does).
这样你不仅可以注释掉差异,还可以添加更多信息(就像stat选项一样)。
Edit: Also git commit --verbosedoes not include the diff to the commit message this way would do without the #s.
编辑:此外git commit --verbose不包括提交消息的差异,这种方式在没有 #s 的情况下也可以做到。
回答by rusty
If you want to always see the diff when you commit, you can add the following to your ~/.gitconfig
file:
如果您想在提交时始终看到差异,可以将以下内容添加到您的~/.gitconfig
文件中:
[alias]
commit = commit -v