git commit 消息中的转义注释字符 (#)

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/22936252/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-19 09:56:40  来源:igfitidea点击:

Escape comment character (#) in git commit message

gitescaping

提问by carlspring

I have set mcedit as my editor for git commit messages. By default it ignores any lines starting with the #character. However odd this may seem, I need to be able to have the my commit message looking like this:

我已将 mcedit 设置为 git commit 消息的编辑器。默认情况下,它会忽略以#字符开头的任何行。不管这看起来多么奇怪,我需要能够让我的提交消息看起来像这样:

#FOO-123: Implement bar foo

Committing work in progress

The #FOO-123: ...is actually the key + title of an issue in our tracker. The tracker can automatically pick up these commit messages and add them to the issue.

#FOO-123: ...实际上是在我们跟踪的问题的关键+称号。跟踪器可以自动获取这些提交消息并将它们添加到问题中。

Unfortunately, the first line gets treated like a comment and gets ignored.

不幸的是,第一行被视为注释而被忽略。

I don't want to have to be committing from the command line by adding -mas it's inconvenient/ugly when you have multiple lines.

我不想通过添加从命令行提交,-m因为当你有多行时它不方便/丑陋。

How could I work around this?

我该如何解决这个问题?

回答by VonC

You can try and define a different character for comments in commit message:

您可以尝试为提交消息中的注释定义不同的字符:

git config core.commentchar <another char>

As I mention in "Start a git commit message with a hashmark (#)", this setting is available since git 1.8.2 (February 2013).

正如我在“使用哈希标记 ( #)启动 git 提交消息”中提到的,此设置自 git 1.8.2(2013 年 2 月)起可用。

In your case:

在你的情况下:

git config core.commentchar "*"

Note that you couldin theory put a core.commentchar word (multiple characters), but git 2.0.x/2.1 will be stricter.

请注意,理论上您可以放置一个 core.commentchar 词(多个字符),但 git 2.0.x/2.1 会更严格。

See commit 50b54fdby Nguy?n Thái Ng?c Duy (pclouds):

提交50b54fd通过Nguyñ泰国伍Ç维战(? )pclouds

config: be strict on core.commentChar

配置:对 core.commentChar 严格

We don't support comment strings(at least not yet). And multi-byte character encoding could also be misinterpreted.

The test with two commas is updated because it violates this. It's added with the patch that introduces core.commentCharin eff80a9(Allow custom "comment char" - 2013-01-16). It's not clear to me whythat behavior is wanted.

我们不支持注释字符串(至少现在不支持)。多字节字符编码也可能被误解。

更新了两个逗号的测试,因为它违反了这一点。它与eff80a9引入core.commentChar的补丁一起添加(允许自定义“评论字符” - 2013-01-16)。我不清楚为什么需要这种行为。



pokealso mentions in the commentsto change the commit template:

poke还在评论中提到更改提交模板

When editing the commit message, start the editor with the contents in the given file.
The commit.templateconfiguration variable is often used to give this option implicitly to the command.

编辑提交消息时,使用给定文件中的内容启动编辑器。
所述commit.template配置变量经常被用于隐含地给这个选项添加到命令。



Note that Git 2.23 (Q3 2019) fixes a bug, as "git interpret-trailers" always treated '#' as the comment character, regardless of core.commentCharsetting, which has been corrected.

请注意,Git 2.23(2019 年第 3 季度)修复了一个错误,因为“ git interpret-trailers”始终将 ' #' 视为注释字符,无论core.commentChar设置如何,已更正。

See commit 29c83fc(19 Jun 2019) by Jeff King (peff).
(Merged by Junio C Hamano -- gitster--in commit 3a50607, 09 Jul 2019)

请参阅Jeff King ( ) 的commit 29c83fc(2019 年 6 月 19 日(由Junio C Hamano合并-- --提交 3a50607 中,2019 年 7 月 9 日)peff
gitster

interpret-trailers: load default config

The interpret-trailersprogramdoes not do the usual loading of config via git_default_config(), and thus does not respect many of the usual options.
In particular, we will not load core.commentChar, even though the underlying trailer code uses its value.

This can be seen in the accompanying test, where setting core.commentCharto anything besides "#" results in a failure to treat the comments correctly.

解释拖车:加载默认配置

interpret-trailers程序不会通过 执行通常的配置加载git_default_config(),因此不尊重许多通常的选项。
特别是,我们不会 load core.commentChar,即使底层的预告片代码使用它的值。

这可以在随附的测试中看到,其中设置core.commentChar为除“ #”之外的任何内容都会导致无法正确处理评论。