'git commit: pre-populate commit subject but still prompt for a commit message [duplicate]
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.
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.
Solution 1:[1]
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
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
Solution | Source |
---|---|
Solution 1 | dave mankoff |