Git Reference ยท Hooks
prepare-commit-msg hook
Reference for the prepare-commit-msg hook: its three arguments, the message source values, how to prefill a ticket id or template, and how it differs from commit-msg.
prepare-commit-msg runs after Git has built the default commit message and before the editor opens. It receives the path of the file that holds the message, so the hook can edit that file in place: prefix a ticket number taken from the branch name, add a template, insert a Signed-off-by line, or append a reminder in comments.
Unlike commit-msg, which judges the final text, this hook prepares the starting text. The person can still change everything in the editor, and -m messages are passed through the hook too.
#!/bin/sh # .git/hooks/prepare-commit-msg msg_file=$1; source=$2; sha=$3
Try it
Press Run (or Ctrl + Enter) to see the result here.
Program input ยท what you'd type while the program runs
Type the answers before you press Run, one per line. Each time the program reads a line (with read or cat), it gets the next line from this box.
Press Run (or Ctrl + Enter) to see the result here.
Program input ยท what you'd type while the program runs
Type the answers before you press Run, one per line. Each time the program reads a line (with read or cat), it gets the next line from this box.
Syntax
.git/hooks/prepare-commit-msg <message-file> [<source> [<commit-id>]]
| Argument | Description |
|---|---|
message-file |
Path of the file containing the proposed message (normally .git/COMMIT_EDITMSG). Edit it in place. |
source |
Where the message came from, or absent when the editor starts from the default. |
commit-id |
Only with source=commit: the commit whose message is reused (-c, -C, --amend). |
The source values are:
| Value | When |
|---|---|
message |
-m, -F or a message given another way. |
template |
-t or the commit.template setting. |
merge |
A merge, or when .git/MERGE_MSG exists. |
squash |
git merge --squash or .git/SQUASH_MSG exists. |
commit |
-c, -C or --amend; the third argument is the commit. |
| (none) | A plain git commit with the editor. |
Exit status: non-zero aborts the commit. Output goes to stderr and is shown to the person.
Usage notes
- Guard against double edits.
git commit --amendreruns the hook withsource=commit, and an editor session may start from a message that already has your prefix. Check before prepending, or skipcommit,mergeandsquashsources as the example does. - Hooks run even with
-m. To leave explicit messages alone, check formessageand exit early, but note that adding structured trailers is often wanted precisely there. - The hook cannot see the user's final edits; use
commit-msgfor validation. Text you write into the file is subject tocommit.cleanup: lines starting with#are removed by the default cleanup mode in an editor session. --no-verifydoes not skip this hook. Onlypre-commitandcommit-msgare bypassed by that option.- For a consistent layout you can use
commit.templateinstead; reach for the hook when the content depends on the branch, the user or the staged files. - Atomic edits: write to a temporary file and
mvit over the message file, as the example does, so an error cannot leave a half-written message.