The post-commit hook: reactions that cannot undo a commit

A post-commit hook is a program git runs after the commit object exists and HEAD has moved to it. By the time it starts, nothing can be approved or refused: the commit is already part of the history. That makes the hook a reaction, not a check. It writes a log, notifies a service, regenerates a file derived from the history or warms a cache. The checks that can stop a commit belong to the pre-commit hook, and the two hooks together are the subject of pre-commit and post-commit together.

The examples below were run with git 2.54.1. The part that mentions the pre-commit framework was run with pre-commit 4.6.2 as well.

What the hook receives

The hook gets no arguments and no input on standard input. Everything it needs is in the repository it runs in: git starts it with the repository root as the working directory, with GIT_INDEX_FILE pointing at the index, and with HEAD already on the new commit. A hook that prints its context:

vi .git/hooks/post-commit

#!/usr/bin/env bash echo "post-commit: arguments: $#" >&2 echo "post-commit: GIT_DIR: ${GIT_DIR:-unset}" >&2 echo "post-commit: GIT_INDEX_FILE: ${GIT_INDEX_FILE:-unset}" >&2 echo "post-commit: HEAD: $(git rev-parse --short HEAD)" >&2 echo "post-commit: message: $(git log -1 --pretty=%s)" >&2 echo "post-commit: worktree: $(pwd)" >&2

The file has to be executable, and then a commit runs it:

chmod +x .git/hooks/post-commit

git commit -m "Extend app.txt"

post-commit: arguments: 0 post-commit: GIT_DIR: unset post-commit: GIT_INDEX_FILE: .git/index post-commit: HEAD: 03a3e9a post-commit: message: Extend app.txt post-commit: worktree: /home/user/git-demo/receives [main 03a3e9a] Extend app.txt 1 file changed, 1 insertion(+)

Three details answer most of the questions people ask about this hook. The argument count is zero, so there is no file name and no message to read from the arguments; the commit is found through HEAD and the message through git log -1. The working directory is the repository root, so a relative path in the hook means a path in the repository. And HEAD is already the new commit, not its parent, which is what makes a hook that reads the history useful.

The same hook runs on git commit --amend and on the commit that concludes a conflicted merge, because both go through git commit. A clean git merge, which writes the merge commit itself, does not run it. It does not run when a commit fails, and git commit --no-verify does not skip it: the flag skips the hooks that guard the commit, and this one is not a guard.

Why it cannot undo the commit

A commit is written in this order: git builds the tree, writes the commit object, updates the branch to point at it, and only then runs post-commit. The object and the ref are final before the hook gets control. The exit status of the hook is ignored, so even a hook that fails leaves a successful commit behind:

#!/usr/bin/env bash echo "post-commit: the commit $(git rev-parse --short HEAD) already exists" >&2 exit 1

git commit -m "Add app.txt"

post-commit: the commit f8567b0 already exists [main (root-commit) f8567b0] Add app.txt 1 file changed, 1 insertion(+) create mode 100644 app.txt

echo $?

0

The command exits 0 and git log shows the commit. The hook printed a line and failed; git recorded neither the failure nor the exit code.

A hook that edits the working tree cannot reach the commit either. This one appends a line to a tracked file after every commit:

#!/usr/bin/env bash printf "rewritten by the hook\n" >> app.txt

The commit contains what was staged, and the worktree contains the staged content plus the line the hook added. The two copies differ, and git status reports a normal uncommitted change:

git show HEAD:app.txt

first line second line

cat app.txt

first line second line rewritten by the hook

git status --short

M app.txt

git show --stat --oneline HEAD

d3294ff Extend app.txt app.txt | 1 + 1 file changed, 1 insertion(+)

Only a new command can replace a commit: git commit --amend, git rebase or git reset. A hook can run one of them, and then it is not reacting to the commit any more, it is rewriting history from inside the point that was supposed to be the end of the operation. Keep the reaction to things that do not change the past.

A hook that regenerates a file

The typical job for this hook is a file derived from the history: CHANGELOG.md, a build stamp, a list of the commits in the last release. Here the file is built from git log after every commit:

vi .git/hooks/post-commit

#!/usr/bin/env bash git log --pretty=format:'- %h %s' > CHANGELOG.md echo "post-commit: CHANGELOG.md now lists $(git rev-list --count HEAD) commits" >&2

chmod +x .git/hooks/post-commit

git commit -m "Extend app.txt"

post-commit: CHANGELOG.md now lists 2 commits [main 1075c48] Extend app.txt 1 file changed, 1 insertion(+)

cat CHANGELOG.md

- 1075c48 Extend app.txt - 4a0606d Start the project

git status --short

?? CHANGELOG.md

The hook replaced the file with a fresh list, and the list already contains the commit that started the hook, because the commit existed before the hook ran. That is the advantage over a pre-commit hook for this job: a pre-commit hook cannot name the commit it is preparing.

The file is untracked. Git did not add it to the commit, because the commit was finished before the hook wrote the file, and the next step is to get it into a later commit.

How the file lands in the next commit

There are two ways, and they differ in who stages the file. The first is to stage it by hand before the next commit:

git add CHANGELOG.md

git commit -m "Add the changelog"

post-commit: CHANGELOG.md now lists 3 commits [main 421a82f] Add the changelog 1 file changed, 3 insertions(+) create mode 100644 CHANGELOG.md

git show HEAD:CHANGELOG.md

- 1075c48 Extend app.txt - 4a0606d Start the project

cat CHANGELOG.md

- 421a82f Add the changelog - 1075c48 Extend app.txt - 4a0606d Start the project

The committed copy is the one that was staged before the commit; the hook then regenerated the file again, and the version on disk now has one entry more. This is not a bug in the hook, it is the order of events: the commit cannot contain a line about itself. The changelog in a commit is always one commit behind the history it describes.

The second way is to stage the file inside the hook. The commit that is being made is already closed, but the index is not: an git add in the hook prepares the file for the next commit instead of the current one:

#!/usr/bin/env bash git log --pretty=format:'- %h %s' > CHANGELOG.md git add CHANGELOG.md

After a commit that changed only app.txt, the index already holds the regenerated changelog:

git commit -m "Extend app.txt again"

[main d2dc2e1] Extend app.txt again 1 file changed, 1 insertion(+)

git status --short

M CHANGELOG.md

git diff --cached --stat

CHANGELOG.md | 2 ++ 1 file changed, 2 insertions(+)

The staged change is visible with two letters in git status: the M in the first column is the index, the empty second column is the worktree. The next commit carries it even though nobody staged it by hand:

git commit -m "Record the changelog"

[main b8f8a54] Record the changelog 1 file changed, 3 insertions(+)

git show --stat --oneline HEAD

b8f8a54 Record the changelog CHANGELOG.md | 2 ++ 1 file changed, 2 insertions(+)

A hook that stages files leaves the index dirty after every commit, and the next commit, whatever it was about, carries the generated file. That is convenient for a file nobody edits, and surprising for a file people do. If the generated file must not be in the repository at all, write it to an ignored path such as build/ and add that directory to .gitignore; nothing is staged, and nothing can end up in a commit by accident.

The pre-commit framework runs its own post-commit stage and stashes unstaged changes around it, which turns a hook that stages a file into a data-loss case; the incident in the workflow article walks through it.

A file that must be committed and is built from the history has a better home than a hook. A script that rebuilds it from the first-parent commits of main, locks it and commits only that file does the job without the problems above.

When it is the right tool

The hook lives in .git/hooks, which is not part of the repository, so a clone starts without it. A tracked folder plus core.hooksPath shares it with the team, as the hooks overview describes, and the framework installs the same two files for its own stages.

Article author: Arthur Isaev