How to add project information to a Git commit comment?

With Git, if you commit, it includes a section under the commit message, which is commented out. This contains instructions for writing a commit message, as well as a list of files that change. Like this:

# 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: # (use "git reset HEAD <file>..." to unstage) # # modified: important-file.txt # # Changed but not updated: # (use "git add <file>..." to update what will be committed) # (use "git checkout -- <file>..." to discard changes in working directory) # # modified: some-other-thing.txt # # Untracked files: # (use "git add <file>..." to include in what will be committed) # # untracked.txt 

Can I add additional material to this file? One explicit use case for those who use problem tracking software, such as Trac , Redmine, and the like, specifying additional syntax elements. For example, Redmine users may include a problem number and some special keywords to resolve the problems resolved: the keywords “refs” (and “links”) and “fixes” (or “close”), but it is often difficult for me to remember key words.

It would be convenient if you could add a specific project at the bottom of the commit commands.

Is there any existing way to do this or do I need to hack it? As a side question, are any other VCS (like Mercurial) similar?

+4
source share
4 answers

I used the commit.template file (as described here ). I could use hook-commit-commg, but the commit pattern makes things easier, plus I can save the commit pattern in version control for the project.

+4
source

Option 1:

As mentioned, the direct way to do this with Git is with hook-commit-message .

However, there are problems with this strategy that need to be considered. From chapter 7.4 Pro Git:

[Client side scripts] arent passed with the project clone, you have to distribute these scripts differently, and then your users copy them into their .git / hooks and make them an executable. You can distribute these hooks within the project or in a separate project, but there is no way to automatically configure.

Option 2:

As mentioned by Tom Morris , use a commit pattern .

Option 3:

You can make a custom Git build that includes your additional instructions. The instructions included in the current commit message are hardcoded in the Git source code. See $GIT_SRC/builtin/commit.c starting at line 655 .

This method is probably not preferred since you will have to apply the patch every time a new version of Git is released.

Option 4:

Create a patch for Git that adds this feature and sends it to the mailing list. If you (or others) decide to try this, I will first ask for advice from the list on how to proceed.


Mercurial:

Hook scripts in Mercurial behave similarly. From Chapter 10 Mercurial: The Complete Guide:

In Mercurial, interceptors are not reviewed, controlled, or distributed when you clone or retrieve from the repository. The reason for this is simple: the hook is a completely arbitrary part of the executable code. It works under your user identity, with your privilege level on your machine.

It would be extremely reckless for any distributed version control system to intercept with control, as this is a suitable way to undermine the user accounts of the revision of the control system.

+3
source

You can configure the commit message using prepare-commit-message hook .

+1
source

I am sure you can use hook-commit-msg for this. See githooks for more information.

0
source

All Articles