Skip to content
Tutorials Center
My learning

Topics

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.

Terminal
#!/bin/sh
# .git/hooks/prepare-commit-msg
msg_file=$1; source=$2; sha=$3

Try it

Bash The arguments for every message source

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.

Bash Add a ticket number from the branch name

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

Text
.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 --amend reruns the hook with source=commit, and an editor session may start from a message that already has your prefix. Check before prepending, or skip commit, merge and squash sources as the example does.
  • Hooks run even with -m. To leave explicit messages alone, check for message and exit early, but note that adding structured trailers is often wanted precisely there.
  • The hook cannot see the user's final edits; use commit-msg for validation. Text you write into the file is subject to commit.cleanup: lines starting with # are removed by the default cleanup mode in an editor session.
  • --no-verify does not skip this hook. Only pre-commit and commit-msg are bypassed by that option.
  • For a consistent layout you can use commit.template instead; 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 mv it over the message file, as the example does, so an error cannot leave a half-written message.