pre-commit and post-commit together: a gate and a reaction

A repository usually wants two different things from its hooks. Before a commit is written it wants a gate: a check that refuses bad content. After the commit exists it wants a reaction: a changelog, a build stamp, a notification. Both fit in one pre-commit configuration, and that is where the trouble starts. The framework saves unstaged work to a patch file before every stage, including the post-commit one, and puts it back afterwards. A reaction that rewrites or stages a file collides with that patch.

The case below is an incident, not a theory: unstaged work disappeared from the working tree, stayed only in a file under ~/.cache/pre-commit, and git commit still exited 0. It was first seen on 2026-11-03 with pre-commit 4.6.2 and reproduced again on 2026-11-13 with git 2.54.1 and pre-commit 4.6.2.

A gate and a reaction

The two hooks see the same repository at two different moments, and that difference decides everything they can do.

The two hooks of a commit
pre-commitpost-commit
Whenafter the index is final, before the commit objectafter the commit object and the branch update
Can stop the commityes, a non-zero exit refuses itno, the exit status is ignored
Skipped by --no-verifyyesno
Good forlint, tests, message rulesregenerated files, notifications

The gate is skippable and the reaction is not, which sounds backwards until you remember what each one can cost. A skipped gate costs a bad commit that CI or a server hook can still refuse; a skipped reaction costs a file that never got written. The pre-commit hook and the post-commit hook cover each one on its own page.

One configuration, two stages

Both hooks are entries in .pre-commit-config.yaml with different stages. The gate here is the whitespace check from the pre-commit article; the reaction regenerates CHANGELOG.md. Local hooks keep the example offline; a real project usually points each entry at a repository with a pinned rev.

vi .pre-commit-config.yaml

repos: - repo: local hooks: - id: whitespace name: check trailing whitespace entry: bash -c 'git diff --cached --check' language: system pass_filenames: false stages: [pre-commit] - id: changelog name: regenerate CHANGELOG.md entry: bash -c 'git log --pretty=format:"- %h %s" > CHANGELOG.md' language: system pass_filenames: false stages: [post-commit] always_run: true

always_run: true and pass_filenames: false belong to the reaction: at the post-commit stage there are no changed files to pass, and the hook has to run on every commit. Two install commands write the two hook files:

pre-commit install

pre-commit installed at .git/hooks/pre-commit

pre-commit install --hook-type post-commit

pre-commit installed at .git/hooks/post-commit

The reaction runs even when there is nothing unstaged, and then it simply reports that it changed a file:

git commit -m "Add app.txt and a changelog"

check trailing whitespace................................................Passed regenerate CHANGELOG.md..................................................Failed - hook id: changelog - files were modified by this hook [main (root-commit) 2c58e88] Add app.txt and a changelog 3 files changed, 18 insertions(+)

That is the healthy case: the hook rewrote the file, the framework reported it, and the commit was made. Nothing was lost, because there was no unstaged work to lose. The next section adds the work.

The incident: work left in a patch file

The framework wants the hooks to see a clean worktree, because a linter that reads files from disk must see the staged content. So before each stage it does the same four things: saves every unstaged change into ~/.cache/pre-commit/patch<timestamp>-<pid>, runs git checkout -- . to bring the worktree back to the index, runs the hooks, and re-applies the patch with git apply. If that apply fails, it runs git checkout -- . again and applies once more.

It does this at the post-commit stage too, although the commit is already finished and the only thing that can change is the worktree. The state that triggers the incident is ordinary: a tracked file with an edit that is not staged, and another file staged for the commit.

git status --short

M CHANGELOG.md M app.txt

CHANGELOG.md has a hand-written draft line that is not staged; app.txt is staged. The reaction then rewrites CHANGELOG.md from git log, which is exactly the file the patch is holding. Whether the work survives depends on one line of the hook: does it stage its own rewrite with git add?

Case A: the hook only writes

The reaction from the configuration above ends with > CHANGELOG.md and nothing else. The commit with the unstaged draft in place:

git commit -m "Extend app.txt"

[WARNING] Unstaged files detected. [INFO] Stashing unstaged files to /home/user/.cache/pre-commit/patch1794601549-1012. check trailing whitespace................................................Passed [INFO] Restored changes from /home/user/.cache/pre-commit/patch1794601549-1012. [WARNING] Unstaged files detected. [INFO] Stashing unstaged files to /home/user/.cache/pre-commit/patch1794601550-1031. regenerate CHANGELOG.md..................................................Failed - hook id: changelog - files were modified by this hook [WARNING] Stashed changes conflicted with hook auto-fixes... Rolling back fixes... [INFO] Restored changes from /home/user/.cache/pre-commit/patch1794601550-1031. [main 9022387] Extend app.txt 1 file changed, 1 insertion(+)

Two stashes, one per stage. The reaction failed with «files were modified by this hook», which is the framework's way of saying that the hook left files different from the ones it started with. The «Rolling back fixes» line is the retry: the apply had failed against the hook's rewrite, so the framework restored the worktree to the index and applied the patch again. The second apply succeeded.

The result: the unstaged work is back, the hook's rewrite is gone.

git status --short

M CHANGELOG.md

cat CHANGELOG.md

- (old changelog) - draft: release notes for 1.1

git show --stat --oneline HEAD

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

The draft line is in the worktree, the generated changelog is not, and the commit contains only app.txt. The command exited 0. The cost of case A is the reaction's own output: it is thrown away, and the next commit will regenerate it, or not, depending on whether the hook runs again.

Case B: the hook writes and stages

Now the same reaction ends with git add CHANGELOG.md:

- id: changelog name: regenerate CHANGELOG.md entry: bash -c 'git log --pretty=format:"- %h %s" > CHANGELOG.md && git add CHANGELOG.md' language: system pass_filenames: false stages: [post-commit] always_run: true

Same state, same commit. The gate passes, the reaction passes, and then the patch cannot be applied:

git commit -m "Extend app.txt"

[WARNING] Unstaged files detected. [INFO] Stashing unstaged files to /home/user/.cache/pre-commit/patch1794601552-1108. check trailing whitespace................................................Passed [INFO] Restored changes from /home/user/.cache/pre-commit/patch1794601552-1108. [WARNING] Unstaged files detected. [INFO] Stashing unstaged files to /home/user/.cache/pre-commit/patch1794601552-1127. regenerate CHANGELOG.md..................................................Passed [WARNING] Stashed changes conflicted with hook auto-fixes... Rolling back fixes... An unexpected error has occurred: CalledProcessError: command: ('/home/user/.local/bin/git', '-c', 'core.autocrlf=false', 'apply', '--whitespace=nowarn', '/home/user/.cache/pre-commit/patch1794601552-1127') return code: 1 stdout: (none) stderr: error: patch failed: CHANGELOG.md:1 error: CHANGELOG.md: patch does not apply Check the log at /home/user/.cache/pre-commit/pre-commit.log [main 4f78d1d] Extend app.txt 1 file changed, 1 insertion(+)

The retry runs git checkout -- ., and that is what makes this case different. The hook staged its rewrite, so the index now holds the generated changelog, and git checkout -- . restores the worktree from that index. The second apply meets the hook's version of the file instead of the old one, and fails again. The framework gives up with a CalledProcessError, and git commit still exits 0. The command line inside the error quotes the absolute path of the git executable the run used.

git status --short

M CHANGELOG.md

cat CHANGELOG.md

- 4f78d1d Extend app.txt - db9310b Add app.txt and a changelog

git show HEAD:CHANGELOG.md

- (old changelog)

The worktree holds the hook's rewrite, staged for the next commit. The draft line is not there. It survived only inside the patch file the error names:

cat /home/user/.cache/pre-commit/patch1794601552-1127

diff --git a/CHANGELOG.md b/CHANGELOG.md index 7693d37..e2338c8 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1 +1,2 @@ - (old changelog) +- draft: release notes for 1.1

One hand-written line, in a file whose name nobody will remember. That is the whole damage: the work exists, but not in the repository, and git says the command succeeded. The patch file is not cleaned up after a failure; it stays until somebody deletes it.

Recovering the work

The patch is relative to the committed version of CHANGELOG.md, so it cannot be applied on top of the hook's rewrite. First reset the index and the worktree to HEAD, then apply the patch:

git apply --check /home/user/.cache/pre-commit/patch1794601552-1127

error: patch failed: CHANGELOG.md:1 error: CHANGELOG.md: patch does not apply

git reset -q

git checkout -- .

git apply /home/user/.cache/pre-commit/patch1794601552-1127

git apply prints nothing when it succeeds, and now the draft is back where it started:

git status --short

M CHANGELOG.md

cat CHANGELOG.md

- (old changelog) - draft: release notes for 1.1

The hook's rewrite is gone with the reset. That is acceptable: it is generated, so running the reaction by hand or the next commit produces it again.

pre-commit run --hook-stage post-commit --all-files

regenerate CHANGELOG.md..................................................Passed

The hand-written line is the only thing that could not be regenerated, and it is what the patch restores.

Two habits make the recovery safe. Look at the patch before touching anything else: git apply --check tells you whether it still fits, and cat tells you what is in it. And do not run git checkout -- . or git reset --hard before you have read the patch: the file under ~/.cache/pre-commit is the only copy of the work at that moment. When several patch files are there, the newest one belongs to the last failed run, and the error message names it exactly.

Setting it up so it does not happen

The incident needs three things at once: the framework stashing at the post-commit stage, a hook that rewrites a tracked file, and unstaged work in that same file. Remove any one of them and the work stays put.

The first two are changes to the hook. This configuration keeps the same reaction, but writes it to an ignored build/changelog.txt:

- id: changelog name: regenerate build/changelog.txt entry: bash -c 'mkdir -p build && git log --pretty=format:"- %h %s" > build/changelog.txt' language: system pass_filenames: false stages: [post-commit] always_run: true

git commit -m "Extend app.txt"

[WARNING] Unstaged files detected. [INFO] Stashing unstaged files to /home/user/.cache/pre-commit/patch1794601555-1218. check trailing whitespace................................................Passed [INFO] Restored changes from /home/user/.cache/pre-commit/patch1794601555-1218. [WARNING] Unstaged files detected. [INFO] Stashing unstaged files to /home/user/.cache/pre-commit/patch1794601556-1237. regenerate build/changelog.txt...........................................Passed [INFO] Restored changes from /home/user/.cache/pre-commit/patch1794601556-1237. [main 95ba604] Extend app.txt 1 file changed, 1 insertion(+)

The stash still happens, but the patch no longer contains the file the hook writes, so the restore succeeds and the draft comes back untouched and unstaged:

git status --short

M notes.txt

git status --ignored --short

M notes.txt !! build/

The last option is to drop the framework from the post-commit stage altogether. A plain hook, tracked in the repository and enabled with core.hooksPath, runs without stashing anything: the gate in .githooks/pre-commit, the reaction in .githooks/post-commit.

vi .githooks/post-commit

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

chmod +x .githooks/pre-commit .githooks/post-commit

git config core.hooksPath .githooks

The same commit with unstaged work in notes.txt: no stash messages, no patch, and the draft is still there afterwards.

git commit -m "Extend app.txt"

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

git status --short

M CHANGELOG.md M notes.txt

core.hooksPath replaces the hook directory, so the files the framework installed in .git/hooks are not called any more; moving the reaction there means moving the gate as well, or calling the framework from the tracked gate script.

git commit --no-verify does not avoid the incident. It skips the gate, and the reaction still runs: the same commit printed the same CalledProcessError with the flag as without it, because post-commit hooks are not part of what the flag skips.

Checklist

Article author: Arthur Isaev