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
^^^^^