#
as a comment is not a rule of Git commit messages. Instead, it's a rule for writing commit messages via an editor.
AFAIK this is to allow Git to display instructions and other information to the commit author.
# Please enter the commit message for your changes. Lines starting
# with '#' will be ignored, and an empty message aborts the commit.
# On branch master
# Changes to be committed:
# new file: what
#
This is saved as .git/COMMIT_EDITMSG
. When your editor closes, Git reads that file, strips out the comments (and other special things), and uses what's left as the commit message.
From the git-commit docs...
$GIT_DIR/COMMIT_EDITMSG
This file contains the commit message of a commit in progress. If git commit exits due to an error before creating a commit, any commit message that has been provided by the user (e.g., in an editor session) will be available in this file, but will be overwritten by the next invocation of git commit.
You can, btw, abort a commit by providing no uncommented lines.
If you use git commit -m
, for example, there's no need for any of that and a message starting with a #
is fine.
$ git ci -m '# message with a comment'
[master 7c6a630] # message with a comment
1 file changed, 0 insertions(+), 0 deletions(-)
create mode 100644 that
$ git log
commit 7c6a630a9c84fda585601edef0b18e7a8683dffa (HEAD -> master)
Author: Michael G. Schwern <schwern@pobox.com>
Date: Wed Feb 22 20:51:36 2017 -0800
# message with a comment
$ cat .git/COMMIT_EDITMSG
# message with a comment
git commit
has an option to control this behavior, --cleanup
. There are various modes which strip comments, cleanup whitespace, or do nothing. The default is "Same as strip if the message is to be edited. Otherwise whitespace." This can be controlled with the commit.cleanup
config variable.
You can, if you really want to, use a commit-msg
hook to make sure comments are always stripped.