git 文档对 %B 漂亮打印说明符的 'unwrapped subject and body' 意味着什么

What does the git documentation mean by 'unwrapped subject and body' for %B pretty print specifier

如果您在 the documentation 中搜索 %b 和 %B,除了神秘的“(展开的主题和正文)”之外,没有任何关于它们之间区别的解释。

这是一个来自 repo 的示例提交(使用 git 版本 2.9.3)以及使用 %b 与 %B 打印的不同结果。提交命令是 git commit -m 'Automated version update from Jenkins.'

$ git log -1 origin/master
commit 30ac57e...
Author: Jenkins <email@email.com>
Date:   Wed Jul 12 16:28:41 2017 +0000

    Automated version update from Jenkins.
$ git log -1 --format=%B origin/master
Automated version update from Jenkins.

$ git log -1 --format=%b origin/master

$ 

我不明白为什么 %b 无法生成提交消息体,也不明白为什么 %B(如果它在某种意义上包含 "subject and body")只提供消息体。

%b 和 %B 从日志中漂亮打印的根本区别是什么?

如果你想可靠地只打印最近的提交消息(仅消息),你应该怎么做?我认为它应该是 git log -1 --format=%b origin/master 但这个例子似乎暗示并非如此。 %B 是否可靠地工作,或者短语“(未包装的主题和正文)”是否意味着它可能在某些情况下以某种方式包含主题?

虽然您可以在编辑提交消息时使用任何格式,但通常的格式在第一行包含一个主题,然后是可选的空行和正文。在您的情况下,您只有一行。这意味着该行是主题,可使用 %s.

打印

使用 --format=email 时,此约定的效果 非常 可见。如果您不使用此约定或依赖它的任何内容,如果您只想要完整的提交消息,请忽略 %s%b.

这个是在git commit documentation中提到的:

DISCUSSION

Though not required, it’s a good idea to begin the commit message with a single short (less than 50 character) line summarizing the change, followed by a blank line and then a more thorough description. The text up to the first blank line in a commit message is treated as the commit title, and that title is used throughout Git. For example, git-format-patch[1] turns a commit into email, and it uses the title on the Subject line and the rest of the commit in the body.

您正在查看的提交 没有 body,因此 %b 正确地不打印任何内容。 "subject",在同一文档的其他地方也称为 "title line",是 headers 和下一个空行之间的所有内容,这是您提交的唯一消息。 body 是从第一个空行开始的所有内容。

用不同的提交进行说明:

$ git log -1 51e6467fdc073a9a5149b4c12ca9d79d6ac46872^!
commit 51e6467fdc073a9a5149b4c12ca9d79d6ac46872
Author: Andrew Rodland <andrew@cleverdomain.org>
Date:   Fri Mar 14 14:22:02 2014 -0400

    Make the event data be the master that owns the socket, instead of the socket

    Then we will be able to access the master's domains and other info later
    on

$ git log --format=%B 51e6467fdc073a9a5149b4c12ca9d79d6ac46872^!
Make the event data be the master that owns the socket, instead of the socket

Then we will be able to access the master's domains and other info later
on

$ git log --format=%b 51e6467fdc073a9a5149b4c12ca9d79d6ac46872^!
Then we will be able to access the master's domains and other info later
on

"Unwrapped" 只是意味着主题将有任何 line-wrapping 删除(尽管惯例是它不应该比一行长,git 不强制执行).

当commit message有Subject和Body的时候可以看出%B和%b的区别

喜欢

git commit

然后输入带有主题和正文的提交消息

git log -1 --format=%B origin/master

这提供了提交消息的主题和正文

git log -1 --format=%b origin/master

这仅提供提交消息的正文。

当使用命令给出提交消息时

git commit -m "Automated version update from Jenkins."

此邮件 "Automated version update from Jenkins." 将被视为主题,使用 %b 将无法生成正文。