gitextensions icon indicating copy to clipboard operation
gitextensions copied to clipboard

: `#issue` lines skipped when using commit template

Open AlexNek opened this issue 3 months ago • 13 comments

Environment

Environment

  • Git Extensions 5.2.1.18061
  • Build 0d74cfdc312df90114a0cf8d6c8a30ca4213e2ff
  • Git 2.47.0.windows.2
  • Microsoft Windows NT 10.0.22631.0
  • .NET 8.0.20
  • DPI 192dpi (200% scaling)
  • Portable: False

Issue description

🐞 Bug Report: #issue lines skipped when using commit template

Summary
When using a commit template, Git Extensions skips lines starting with #, assuming they are comments. However, in our workflow, we use #123 to reference issue numbers. These lines are unintentionally ignored, which causes loss of important commit message content.

Code Reference

// When a committemplate is used, skip comments and do not count them as line.
// otherwise: "#" is probably not used for comment but for issue number
if (usingCommitTemplate && line.StartsWith("#"))
{
    continue;
}

Expected Behavior
Lines like #123 should be preserved in the commit message, even when a commit template is used. These are not comments but issue references.

Actual Behavior
Lines starting with # are skipped entirely when usingCommitTemplate is true.

Suggested Fix
Consider distinguishing between actual comments and issue references. For example:

  • Only skip lines starting with # (hash followed by space)
  • Or allow configuration to define comment prefix

Steps to reproduce

  1. Create a commit template file
    Save a file named commit-template.txt with the following content:

    #123 Fixed login bug
    
  2. Configure Git Extensions to use the template

    • Open Git Extensions.
    • Go to SettingsGit Config.
    • set the Path to commit template to point to your commit-template.txt file.
  3. Open the Commit Dialog

    • Stage any change in your repository.
    • Click Commit to open the commit message window.
  4. Observe the behavior

    • The line #123 Fixed login bug is missing from the commit message.
    • Git Extensions skips it, assuming it's a comment due to the leading #.

we are using hooks to check commit format and hooks receive empty file

Did this work in previous version of GitExtensions?

No response

Diagnostics

No response

AlexNek avatar Sep 26 '25 21:09 AlexNek

That's standard git behavior

In many parts of Git, the (hash) character is treated as a comment character. Any text following a on a line will be ignored when the file is parsed by Git. [1, 2, 3, 4, 5]
The specific contexts where this behavior applies include:

• Commit messages: When writing a commit message in an editor, Git will ignore any lines that begin with . The default commit message template, for example, is full of commented-out hints and instructions. This can be an issue if a team uses a ticket-tracking system where issue numbers begin with . To work around this, you can configure Git to use a different comment character. • .gitignore files: In a .gitignore file, a line starting with is treated as a comment and does not affect which files are ignored. If you need to ignore a file that literally starts with , you must escape the character with a backslash (). • git config files: Configuration files for Git, such as .git/config or ~/.gitconfig, treat lines starting with or as comments. • Interactive rebase: During an interactive rebase (), the to-do list that opens in your editor treats lines beginning with as comments. [1, 2, 6, 7, 8]

Changing the comment character If you need to use at the beginning of a line for something other than a comment (such as a ticket ID in a commit message), you can change Git's default comment character to another single character. [9]
To change the comment character for a single commit, use the flag: To change it globally for all your repositories, use the command: [10, 11]

AI responses may include mistakes.

[1] https://stackoverflow.com/questions/2788092/start-a-git-commit-message-with-a-hashmark [2] https://git-scm.com/docs/gitignore [3] https://www.kenmuse.com/blog/what-is-gitattributes/ [4] https://coding-for-reproducible-research.github.io/CfRR_Courses/individual_modules/introduction_to_version_control/ignoring_files.html [5] https://techblost.com/how-to-setup-gitignore-file/ [6] https://www.kevinkuszyk.com/2016/03/09/git-tips-3-changing-the-git-rebase-comment-character/ [7] https://dev.to/1407arjun/how-to-use-git-rebase-to-alter-previous-commits-in-the-project-history-3120 [8] https://stackoverflow.com/questions/21297970/can-i-comment-out-a-line-in-a-git-config-file [9] https://stackoverflow.com/questions/64958535/is-it-possible-to-escape-the-hash-comment-charactor-in-git-commit-messages [10] https://blog.webdevsimplified.com/2021-10/advanced-git-commands/ [11] https://docs.syntevo.com/SmartGit/Latest/Manual/GUI/Preferences/Commands

vbjay avatar Sep 26 '25 21:09 vbjay

Why not replace "#" to "# " then both party will be happy

Changing the comment character If you need to use at the beginning of a line for something other than a comment

Will not working with gitextensions there fixed check for "#"

AlexNek avatar Sep 26 '25 21:09 AlexNek

Why not replace "#" to "# " then both party will be happy

Did you not see

Changing the comment character If you need to use at the beginning of a line for something other than a comment (such as a ticket ID in a commit message), you can change Git's default comment character to another single character. [9] To change the comment character for a single commit, use the flag: To change it globally for all your repositories, use the command: [10, 11]

Git extensions is a gui over git. You are fighting default git behavior. I did give you info around how to change it in cli.

vbjay avatar Sep 26 '25 21:09 vbjay

Changing the comment character If you need to use at the beginning of a line for something other than a comment

Did you not see the code? src/app/GitCommands/CommitMessageManager.cs

// When a committemplate is used, skip comments and do not count them as line.
// otherwise: "#" is probably not used for comment but for issue number
if (usingCommitTemplate && line.StartsWith("#"))
{
    continue;
}

I need to to use such template commit "#234 | text" gitextension will be always skip it

Git extensions is a gui over git.

Not exactly :(

AlexNek avatar Sep 26 '25 21:09 AlexNek

Changing the comment character If you need to use at the beginning of a line for something other than a comment

Did you not see the code?

// When a committemplate is used, skip comments and do not count them as line.
// otherwise: "#" is probably not used for comment but for issue number
if (usingCommitTemplate && line.StartsWith("#"))
{
    continue;
}

I need to to use such template commit "#234 | text" gitextension will be always skip it

Which mimics git functionality. Although @RussKie I see room for improvement to read the git config value for the correct comment character and not hard coding #.

vbjay avatar Sep 26 '25 21:09 vbjay

See https://git-scm.com/docs/git-config#Documentation/git-config.txt-corecommentChar

vbjay avatar Sep 26 '25 21:09 vbjay

I see room for improvement to read the git config value for the correct commit character and not hard coding #.

yes it is another bug but how much time you will need for such correction?

In your link comments looks like starting with "# "

[core]
# single character for older versions
commentChar = "#"
# string for newer versions (which will override commentChar
# because it comes later in the file)
commentString = "//"

With your suggestion, it will take a very long time to use Git extensions in a similar situation.

  1. I need to understand the problem of why my template doesn't work.
  2. Update the comment character.
  3. Wait for the Git Extensions to update the comment character correctly.

AlexNek avatar Sep 26 '25 21:09 AlexNek

I see room for improvement to read the git config value for the correct commit character and not hard coding #.

yes it is another bug but how much time you will need for such correction?

In your link comments looks like starting with "# "

[core]
# single character for older versions
commentChar = "#"
# string for newer versions (which will override commentChar
# because it comes later in the file)
commentString = "//"

Wrong. It's a character. Which is #.

Full documentation

core.commentChar core.commentString Commands such as commit and tag that let you edit messages consider a line that begins with this character commented, and removes them after the editor returns (default #).

If set to "auto", git-commit would select a character that is not the beginning character of any line in existing commit messages.

Note that these two variables are aliases of each other, and in modern versions of Git you are free to use a string (e.g., // or ⁑⁕⁑) with commentChar. Versions of Git prior to v2.45.0 will ignore commentString but will reject a value of commentChar that consists of more than a single ASCII byte. If you plan to use your config with older and newer versions of Git, you may want to specify both:

[core]

single character for older versions

commentChar = "#"

string for newer versions (which will override commentChar

because it comes later in the file)

commentString = "//"

So basically if your config file had one later than other , last one wins.

vbjay avatar Sep 26 '25 22:09 vbjay

I think you are trying to follow the Naked Theory. Try thinking about the practice. How will Git Extensions support all Git versions?

AlexNek avatar Sep 26 '25 22:09 AlexNek

A pull request with a correction is welcome

PR

I cannot find any related documentation for the comment definitions, as I would like to update the documentation as well.

AlexNek avatar Sep 27 '25 09:09 AlexNek

A pull request with a correction is welcome

PR

I cannot find any related documentation for the comment definitions, as I would like to update the documentation as well.

Not described right now, should be added to https://git-extensions-documentation.readthedocs.io/en/release-6.0/commit.html https://github.com/gitextensions/GitExtensionsDoc/blob/main/source/commit.rst

A lot could be added to the doc...

gerhardol avatar Sep 27 '25 20:09 gerhardol

A lot could be added to the doc...

My problem is that I don't like adding descriptions to the documentation where I want them, so it would be better to add them to the existing parts.I cannot find the 'Commit comments' section, so it could be placed either under 'Comments' in the root directory or under 'Comments' in the 'Commit' section.

AlexNek avatar Oct 01 '25 21:10 AlexNek