Note that, since git1.8.2 (February 2013), you can use a different character than '#
' for the commented line in the commit message.
That allows you to use '#
' for your bug number reference.
Various "hint" lines Git gives when it asks the user to edit messages in the editor are commented out with '#
' by default.
The core.commentChar
configuration variable can be used to customize this '#
' to a different character.
In theory, you could put a core.commentChar
word (multiple characters), but git 2.0.x/2.1 will be stricter (Q3 2014).
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.
git 2.0.x/2.1 (Q3 2014) will add an automatic selection for core.commentChar
:
See commit 84c9dc2
When core.commentChar
is "auto
", the comment char starts with '#
' as in default but if it's already in the prepared message, find another char in a small subset. This should stop surprises because git strips some lines unexpectedly.
Note that git is not smart enough to recognize '#
' as the comment char in custom templates and convert it if the final comment char is different.
It thinks '#' lines in custom templates as part of the commit message. So don't use this with custom templates.
The list of candidate characters for "auto" are:
# ; @ ! $ % ^ & | :
That means a command like git commit -m '#1 fixed issue'
will automatically switch the commentChar to ';
', because '#
' was used in the commit message.
See "Making a hash of things – using #s
in Git commit messages" by Tom Wright
The Stackoverflow answer I linked to above also mentions a feature in Git that will choose a comment character automatically, based on the characters you use in commit messages.
git config --global core.commentChar auto
Sounds great right?
Unfortunately, it only changes the comment character based on commits made after you turn it on; it doesn’t use your commit history to inform the choice.
To my mind, this is a great feature hobbled by poor execution.
It seems like a feature that would be effective only if it were on by default:
- One group of people will simply avoid using hashes in commits because they are familiar with the consequences.
- Others (like us) will only realise they need to change the comment character when they need to do a rebase. It doesn’t make sense in this situation to add a new commit just to trigger the desired behaviour.
- A third group of people will consciously accept early on that they need to change the default comment character and will simply choose an alternative.
In other words, having this feature available as a non-default option helps virtually no-one.
Since having it on by default would do nothing to harm any users, and would remove a pain point for some users, I can’t work out why this isn’t the case.
Git isn’t famed for its usability, but to have a fix available and not to turn it on seems gratuitously user-hostile.
Note: Git 2.41 (Q2 2023) adds:
See commit d3b3419 (27 Mar 2023) by Kristoffer Haugsbakk (LemmingAvalanche
).
(Merged by Junio C Hamano -- gitster
-- in commit 5c93cfd, 31 Mar 2023)
config
: tell the user that we expect an ASCII character
Signed-off-by: Kristoffer Haugsbakk
Commit 50b54fd ("config
: be strict on core.commentChar
", 2014-05-17, Git v2.1.0-rc0 -- merge listed in batch #2) notes that “multi-byte character encoding could also be misinterpreted”, and indeed a multi-byte codepoint (non-ASCII) is not accepted as a valid core.commentChar
.
The message is now:
core.commentChar should only be one ASCII character
^^^^^
This is confirmed with Git 2.45 (Q2 2024), batch 7: The "core.commentChar
" configuration variable only allows an ASCII character, which was not clearly documented before.
See commit fb7c556 (05 Mar 2024) by Kristoffer Haugsbakk (LemmingAvalanche
).
(Merged by Junio C Hamano -- gitster
-- in commit 26ab20c, 14 Mar 2024)
config
: document core.commentChar
as ASCII-only
Reported-by: Manlio Perillo
Signed-off-by: Kristoffer Haugsbakk
d3b3419 ("config
: tell the user that we expect an ASCII character", 2023-03-27, Git v2.41.0-rc0 -- merge listed in batch #6) updated an error message to make clear that this option specifically wants an ASCII character but neglected to consider the config documentation.
git config
now includes in its man page:
messages consider a line that begins with this ASCII character
With Git 2.45 (Q2 2024), batch 15, core.commentChar
used to be limited to a single byte, but has been updated to allow an arbitrary multi-byte sequence.
See commit 9ccf3e9 (27 Mar 2024), and commit 8b31147, commit 103d563, commit 78275b0, commit 7eb35e0, commit 2ec225d, commit 600559b, commit f99e1d9, commit a1bb146, commit 3a35d96, commit 2982b65, commit 72a7d5d, commit 2786d05, commit 1751e58, commit 3b45450, commit db7f930, commit 727565e (12 Mar 2024) by Jeff King (peff
).
(Merged by Junio C Hamano -- gitster
-- in commit dce1e0b, 05 Apr 2024)
config
: allow multi-byte core.commentChar
Signed-off-by: Jeff King
Now that all of the code handles multi-byte comment characters, it's safe to allow users to set them.
There is one special case I kept: we still will not allow an empty string for the commentChar.
While it might make sense in some contexts (e.g., output where you don't want any comment prefix), there are plenty where it will behave badly (e.g., all of our starts_with()
checks will indicate that every line is a comment!).
It might be reasonable to assign some meaningful semantics, but it would probably involve checking how each site behaves.
In the interim let's forbid it and we can loosen things later.
Likewise, the "commentChar cannot be a newline
" rule is now extended to "it cannot contain a newline
" (for the same reason: it can confuse our parsing loops).
git config
now includes in its man page:
(default '#
'). Note that this option can take values larger than
a byte (whether a single multi-byte character, or you
could even go wild with a multi-character sequence).
git config core.commentchar
allows to configure that comment character. See my answer below – Hepatogit commit --cleanup=scissors
will be more flexible. See detail in my answer – Titanismgit config core.commentChar "@"
solved it for me. I rarely want to add E-Mails to git commits – Preoccupy--cleanup-=scissors
from now on. – Hacking