有时,使用详细提交时,git 会包含差异提交消息

有时,使用详细提交时,git 会包含差异提交消息

我在 macOS Mojave 上使用 git 版本 2.16.2(但我在其他版本上也看到过这种情况),并且我观察到了奇怪的行为git commit -v

有时当我写下提交消息并关闭编辑器(在我的情况下是 nano)时,提交消息会发生改变并且提交差异会添加到消息中。

例如:

commit c7f28bc4849318a542fe6312444f3a72d82787f0
Author: Ivan Dokov <***@gmail.com>
Date:   Tue Oct 16 16:33:49 2018 +0300

Firm Notes deletion

diff --git a/src/app/pages/firm/firm-notes/firm-notes.component.html b/src/app/pages/firm/firm-notes/firm-notes.component.html
index 2267393..f0cfed4 100644
--- a/src/app/pages/firm/firm-notes/firm-notes.component.html
+++ b/src/app/pages/firm/firm-notes/firm-notes.component.html
@@ -14,17 +14,38 @@
...

我认为当我之前没有留下空行时就会发生这种情况:

# Please enter the commit message for your changes. Lines starting
# with '#' will be ignored, and an empty message aborts the commit.

这种行为是不一致的。通常我不会留下空行,它也能正常工作,但当我真正看到错误的提交消息时,我会修改提交并添加一个空行,问题就解决了。问题是我在提交后并不总是关注提交消息,并且我会将这些错误的提交消息推送到存储库。

git commit -v不幸的是我是办公室里唯一一个使用它的人,没有其他人发现这样的问题。

这是我的git配置

谷歌也帮不上忙,因为我似乎找不到任何有类似问题的帖子。

更新:我刚刚发现在提交消息后添加一个空行并不总是能解决问题。

答案1

当我这样做时,git commit -v我默认会收到此消息:

# 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:
#   modified:   foo.txt
#
# ------------------------ >8 ------------------------
# Do not modify or remove the line above.
# Everything below it will be ignored.
diff --git a/foo.txt b/foo.txt
index 88545b8..9fcffaf 100644

对于您的情况,这是您不应该修改的部分:

# ------------------------ >8 ------------------------
# Do not modify or remove the line above.
# Everything below it will be ignored.

如果您没有看到这些行,则可能是您的配置git commit方式很奇怪。在这种情况下,请使用此命令检查并git help config查阅git help commit

git config -l |grep "^commit\."

答案2

我遇到了同样的问题。原来我的 nano 编辑器配置为写入DOS format

保存提交时,我使用 更改了格式option + m以将其改回正常格式。这阻止了提交消息包含整个差异。

我肯定在某个时候意外地将配置设置为 DOS 格式。

相关内容