Escape comment character (#) in git commit message

2019-02-06 03:54发布

问题:

This question already has an answer here:

  • Start a git commit message with a hashmark (#) 8 answers

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:

#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.

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 -m as it's inconvenient/ugly when you have multiple lines.

How could I work around this?

回答1:

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).

In your case:

git config core.commentchar "*"

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

See commit 50b54fd by Nguyễn Thái Ngọc Duy (pclouds):

config: be strict on 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.commentChar in eff80a9 (Allow custom "comment char" - 2013-01-16). It's not clear to me why that behavior is wanted.


poke also mentions in the comments to change the commit template:

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



标签: git escaping