54

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
#
gerrit
  • 24,025
  • 17
  • 97
  • 170
zedoo
  • 10,562
  • 12
  • 44
  • 55

4 Answers4

83

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).

Jakub Narębski
  • 309,089
  • 65
  • 217
  • 230
  • 10
    This only changes the commit text, ie what appears before the comment markers. The OP's asking about changing the commit comment markers themselves. – me_and Dec 04 '12 at 19:05
  • 3
    This is really helpful. There are a couple of lines that I had to keep typing in *every* commit I make. Nice! Also I found this vim tip uber useful : [**Position cursor on first line of commit message**](http://vim.wikia.com/wiki/Always_start_on_first_line_of_git_commit_message). Happy Days ! – Ashutosh Jindal Mar 25 '13 at 13:31
  • @me_and: commit comment markers were not configurable at the time of writing this answer. There was proposal to make them configurable, but I don't remember if it is configuration available to user, and whether this change is in released version. – Jakub Narębski Jan 10 '14 at 14:43
  • 2
    Funny thing is this answer doesn't answer the original question, although the original problem is probably solved by it, but nonetheless this answer has the most upvotes! So it seems to me that this is what people are actually looking for (like myself). – Erik Jan 24 '18 at 17:38
48

You can use 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.

doughgle
  • 827
  • 1
  • 9
  • 18
weiqure
  • 3,247
  • 2
  • 27
  • 31
  • I knew it was possible. I read about it somewhere and couldn't find this any more. Thanks! – zedoo Oct 19 '10 at 12:44
  • 2
    Just beware that hooks don't get added to the repo. When this repo is cloned somewhere else, you'll have to set up the hook again. – brycemcd Oct 19 '10 at 15:06
  • 17
    commit.template configuration variable is there exactly for this purpose and that should be used instead. – Jaseem Aug 12 '13 at 06:30
  • I wrote a wrapper to make git hooks handling more simple. https://pypi.org/project/hooks4git/ – Lovato Aug 06 '18 at 19:03
2

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.

Mat
  • 202,337
  • 40
  • 393
  • 406
swayamraina
  • 2,958
  • 26
  • 28
0

Put something like this in .gitconfig (source):

[commit]
  template = ~/myGitMessage.txt

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

T.Todua
  • 53,146
  • 19
  • 236
  • 237