Search code examples
gitvisual-studio-codegit-commit

How to let VS Code's Commit UI support a `#` in the commit message?


I need to follow a git commit message format that starts with a # (ex. #1234 ...).

This is not a duplicate of Start a git commit message with a hashmark (#) because I know about the commentchar and I already configured my git to use a different comment character:

gino@myrepo$ git config --global --get core.commentchar
;
gino@myrepo$ git config --get core.commentchar
;

I've also confirmed that it works if I commit from the command line:

gino@myrepo$ git log
Author: ...
Date:   Wed Nov 13 21:59:40 2019 +0900

    # Test `git commit` from terminal
    # These 2 lines should not be treated as comments

The problem is when I try to do my commits from VS Code's Commit UI:

vscode commit

VS Code's Git does not seem to honor the config that # is not a comment, because when I check the git log, it only shows the 2nd line:

gino@myrepo$ git log
commit 1254416d309588293372b96fd1f71e30af51b1fe (HEAD -> master)
Author: ...
Date:   Wed Nov 13 22:23:17 2019 +0900

    These lines should be details.

It's worse when I try to use a one-line message (#4567: blah). The Commit UI will not allow me to commit (nothing happens when I click on the commit button) and the Command Palette > Git: Commit command will simply abort the commit (probably because it gets treated as an empty commit message).

How do I get VS Code to recognize the custom commentchar setting?

Notes:

  • I know that VS Code uses my git config (global or local) because when I try changing other settings (ex. user.name), it applies it correctly
  • It's not a duplicate of Start a git commit message with a hashmark (#) because I am talking about committing via VS Code's Commit UI, not via the command line
  • I can't find a VS Code > Git setting related to commit messages
  • I'm using VS Code 1.40.0

Solution

  • It was apparently a regression bug with the 1.40.0 version of VS Code.
    (Thanks to @Bauke for pointing me to the relevant Github issue.)

    https://github.com/microsoft/vscode/issues/84201#issuecomment-552830865:

    The fix for #6403 made it that the input box now treats lines starting with # as comments.

    It was then "fixed" as part of the 1.40.1 version of VS Code.
    (Update 1.40.1: The update addresses these issues.)

    https://github.com/microsoft/vscode/issues/84201#issuecomment-552840563

    You should be able to commit a single line starting with # and followed by any contents.

    You should be able to commit messages with lines starting with # followed by digits. Lines starting with # and not followed by digits will be commented out. For example, the following commit message:

    first line
    second line
    #third line
    # fourth line
    # 5th line
    #6th line
    #7 th line
    #8
    

    Should become:

    first line
    second line
    #6th line
    #7 th line
    #8
    

    Since all other lines should be commented out.

    I say "fixed" because from the discussion on that Github issue, it seems that the Commit UI input box does not exactly follow or use git's commentchar config, and that it has its own parsing of which parts of the commit message are comments.

    In my case, since my commit message follows the format "single line starting with # and followed by any contents", then it seems to be work. But if you are using a different format that does not fit the input box's regex rules, then it's not going to work.