Skip to content
Tutorials Center
My learning

Topics

Git Reference ยท Hooks

post-commit hook

Reference for the post-commit hook: when it runs, what it can read about the new commit, why it cannot block anything, and which commands do or do not trigger it.

The post-commit hook runs after a commit has been created and HEAD has moved to it. It takes no arguments and receives no standard input, and its exit status is ignored: the commit is already there, so the hook cannot veto it. That makes it a good place for notifications, logging, updating a build stamp, or kicking off a background job, and a bad place for any check that must pass.

To learn about the new commit, the hook asks Git: git rev-parse HEAD for its id, git log -1 for the message and git diff-tree for the changed files.

Terminal
#!/bin/sh
# .git/hooks/post-commit
echo "committed $(git rev-parse --short HEAD): $(git log -1 --format=%s)" >&2

Try it

Bash Inspect the commit that was just made

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 A failing post-commit hook, and which commands trigger 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.

Syntax

Text
.git/hooks/post-commit
Item Detail
Arguments None.
Standard input None.
Exit status Ignored. A non-zero status does not undo the commit, and git commit still succeeds.
Skipped by Nothing. --no-verify does not affect this hook.
Output Shown to the user on the terminal (stderr).

Edge cases: the hook runs once per created commit, so an operation that creates several commits (such as a rebase) can trigger it several times. Commits created with plumbing commands like git commit-tree never run it, and neither does the merge commit made by git merge; use post-merge for merges. When you need to see every ref change regardless of the command, use reference-transaction.

Usage notes

  • Keep it short. The hook runs synchronously: git commit does not return until it finishes. Start long jobs in the background (nohup ... &) and redirect their output so the commit prompt returns at once.
  • It runs on amends too, and for cherry-pick and revert as the second example shows. Do not assume that each run corresponds to a new piece of work from the keyboard.
  • Cannot enforce anything. For validation use pre-commit or commit-msg. Because the exit status is ignored, an error in the hook is easy to miss: log failures somewhere you will see them.
  • Use --root with git diff-tree so the first commit (which has no parent) lists its files too.
  • Detect interactive use if needed. Notifications are fine for humans but noisy during automated rebases, so scripts often check an environment variable you set for batch jobs.
  • Server-side counterpart: post-receive is the hook to use for deployments and notifications about pushed commits, because a local commit says nothing about what other people see.
  • A hook's own Git commands inherit environment variables such as GIT_DIR; call git -C or unset them if the hook touches a different repository.
  • Typical uses: desktop notifications, appending to a change log, regenerating a version file, updating a local search index, refreshing ctags.