6

I've added some changes on commit message in prepare-commit-msg file and then I exec this command

git config --global commit.template .git/hooks/prepare-commit-msg

After that when I do git commit I receive something like this

40 lines of my changes and then

# Please enter the commit message for your changes. Lines starting
# with '#' will be ignored, and an empty message aborts the commit.   
# Explicit paths specified without -i nor -o; assuming --only paths... 
# On branch master
# Changes to be committed:
#
#       modified:   test
#    

Question is there any chance to show this default message on the top? Or better permanently remove this message?

1 Answer 1

10

You seem to be mixing up the commit.template option (which provides a default value for the --template option to git commit) with the prepare-commit-message hook.

Normally git commit uses the following sequence of operations:

  1. Run the pre-commit hook, if it exists and is runnable. If it exits non-zero, abort the commit.
  2. Copy any specified or configured template (see below) to a temporary file. If there is no template or the template path is not readable, begin with an empty temporary file.
  3. Add the lines # Please enter the commit message ... and the output of git status.
  4. Run the prepare-commit-message hook, if it exists and is runnable, on the temporary file.
  5. Open up your editor on the temporary file. (Your editor is set from $GIT_EDITOR, the core.editor configuration, $VISUAL, $EDITOR, or a built-in default, whichever is the first one set.)
  6. Once you exit your editor, make the commit, or stop the commit, depending on whether you have provided a commit message.

If you use the -f or -m options, steps 2, 3, and 5 are normally skipped (though you can force git to open your editor by adding --edit). Presumably you have not used those options.

What the --template option does—and hence what commit.template does—is to provides the path name of a file that git commit will copy in step 2. This does not affect lines added in step 3. While the path .git/hooks/prepare-commit-message is (probably) a file git can read, it's not a very sensible name for your template, since if that same path is made executable, the file will become runnable and step 4 will probably behave badly.

You can tell git commit not to do step 3 by adding --no-status. (Also, as a somewhat odd side effect, --no-edit, which explicitly suppresses step 5, also suppresses step 3.)

Or, you can make use of step 4 to eliminate some or all of the git status output and standard # Please enter... message. The prepare-commit-message hook can make arbitrary changes to the template file.

Note that --cleanup=<mode> affects what winds up in the final commit message, and also the processing of step 6. For details see the git commit documentation.

2
  • Oh thanks for answer but as for now I don't understand why there can by problem with step 4? Can you explain that? Also I found that command -> > git config --global commit.status false And as I can see this command delete from git commit this template "Please enter..." question is it a good way to do like this? Also how can I push this changes (on config file) to other branches?Or should I change it on master branch?
    – Adrianeo
    Jan 18, 2016 at 20:48
  • 1
    At step 4, git will run the file in .git/hooks/prepare-commit-message, if it can. If you put ordinary text in this file, what happens if git manages to run it? As for commit.status, setting it to false makes --no-status the default, so that's a reasonable way to do it. Meanwhile your configuration (whether --global or local) is not something git changes when changing branches, so there is no issue there.
    – torek
    Jan 19, 2016 at 0:17

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.