How can I change the default comments in the git commit message?

GitGit Commit

Git Problem Overview


Is it possible to modify the commented part of the default git commit message? I want to add a bit more 'context' information for my users.

# Please enter the commit message for your changes.
# (Comment lines starting with '#' will not be included)
# Explicit paths specified without -i nor -o; assuming --only paths...
# On branch master
# Changes to be committed:
#   (use "git reset HEAD <file>..." to unstage)
#
#       modified:   test.txt
#

Git Solutions


Solution 1 - Git

There is commit.template configuration variable, which according to git-config(1) manpage:

> Specify a file to use as the template for new commit messages. "~/" is expanded to the value of $HOME and "~user/" to the specified user's home directory.

You can put it in per-repository (.git/config), user's (~/.gitconfig) and system (/etc/gitconfig) configuration file(s).

Solution 2 - Git

You can use http://git-scm.com/book/en/v2/Customizing-Git-Git-Hooks">git hooks for that. Before the person who wants to commit the changes is shown the commit message, the prepare-commit-msg script is run.

You can find an example prepare-commit-msg script in .git/hooks.

To edit the default message create a new file called prepare-commit-msg in the .git/hooks folder. You can edit the commit message by using a script like this:

#!/bin/sh
echo "#Some more info...." >> $1

The $1 variable stores the file path to the commit message file.

Solution 3 - Git

Here is a python git-hook to clean up the default message. Hook name: prepare-commit-msg.

#!/usr/bin/env python
import sys
commit_msg_file_path = sys.argv[1]
with open(commit_msg_file_path, 'a') as file:
    file.write('')

You can simply add you text in the file.write() method.

Solution 4 - Git

Put something like this in .gitconfig (source):

[commit]
  template = ~/myGitMessage.txt

and in that file content, set your default commit message.

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionzedooView Question on Stackoverflow
Solution 1 - GitJakub NarębskiView Answer on Stackoverflow
Solution 2 - GitweiqureView Answer on Stackoverflow
Solution 3 - GitswayamrainaView Answer on Stackoverflow
Solution 4 - GitT.ToduaView Answer on Stackoverflow