Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Escape comment character (#) in git commit message [duplicate]

Tags:

git

escaping

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?

like image 870
carlspring Avatar asked Apr 08 '14 11:04

carlspring


People also ask

How do you escape a character?

Escape CharactersUse the backslash character to escape a single character or symbol. Only the character immediately following the backslash is escaped. Note: If you use braces to escape an individual character within a word, the character is escaped, but the word is broken into three tokens.

How do you escape special characters in markdown?

The way to escape a special character is to add a backslash before it, e.g., I do not want \_italic text\_ here . Similarly, if # does not indicate a section heading, you may write \# This is not a heading . As mentioned in Section 4.12, a sequence of whitespaces will be rendered as a single regular space.

What is the escape character in HTML?

CSS represents escaped characters in a different way. Escapes start with a backslash followed by the hexadecimal number that represents the character's hexadecimal Unicode code point value. If there is a following character that is not in the range A–F, a–f or 0–9, that is all you need.

What are escape characters and how do you use them?

To insert characters that are illegal in a string, use an escape character. An escape character is a backslash \ followed by the character you want to insert.


1 Answers

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.


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

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

interpret-trailers: load default config

The interpret-trailers program does 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.commentChar to anything besides "#" results in a failure to treat the comments correctly.

like image 146
VonC Avatar answered Sep 18 '22 17:09

VonC