4

Let's say I'm working on a branch and I run git commit. I am then taken to the commit message prompt where I may enter a commit subject and message. What I'm looking for is a way to have the commit subject pre-populated while still being able to write the commit message manually.

This question is distinct from this one in that I want the commit subject to be populated but still want to be prompted to enter a commit message, i.e. running git commit -a in git bash results in something like:

Edit: improper usage of githooks was preventing the accepted answer from the linked question from working. So, the questions are not distinct and this can be marked as a duplicate.

example in git bash

I'm currently trying to pre-populate the commit subject with a branch name, but in the future I may want that text to be something else. As such, a general solution (not specifically for branch names) would be preferred.

3
  • What's the problem with using one of the answers from the other question you linked? You can just create a prepare-commit-msg hook and not fill in the description. Dec 16, 2018 at 17:37
  • The accepted answer isn't loading the commit prompt for me to enter a message.
    – buratino
    Dec 16, 2018 at 17:39
  • Scratch that, there was an error on my end. This is indeed a duplicate and can be marked as such.
    – buratino
    Dec 16, 2018 at 18:09

1 Answer 1

13

The .git/hooks/prepare-commit-msg hook allows you to prepare the commit message before entering the commit message prompt. Within that hook, you receive the commit message as the first argument, and could, e.g., echo into it:

COMMIT_MSG_FILE=$1

branch=$(git rev-parse --symbolic --abbrev-ref HEAD)

echo "Committing into branch: $branch" > $1;
echo "" >> $1;
echo "Some initial commit message body." >> $1;

If you just have some predefined text, and don't need any logic, (such as getting the branch name), you could just use a commit message template. This is a plain old text file, that the commit.template configuration points to. E.g., if your file is ~/.gitmessage:

$ git config commit.template ~/.gitmessage

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