Volume XXII, number 279Tuesday, October 6, 2026Latest message 1 hour ago

The Git List

News and archive of git@vger.kernel.org, since April 2005

Re: [RFC] worktree: add lifecycle hooks

1 messages between Sep 22, 2026 and Sep 22, 2026, from Michał Papis.

Plain Markdown or JSON for tools and agents.

Michał PapisSep 22, 2026, 16:54 UTC on lore

The important piece for your use case is the pre-worktree-remove hook, not just post-operation hooks.

Post-hooks fire after the worktree is already gone. That's fine when a tool's state lives entirely inside the worktree. It breaks when the worktree points at state outside it — a docker container, a database, sockets, volumes. Then the worktree itself is the registry: its config is the only mapping from worktree to external resources, and pre-remove is the only moment that mapping is still readable. After removal you're guessing — hunting the disk for orphans, diffing against remaining worktrees, turning deterministic teardown into stale-data detection.

Real example: with AI-generated worktrees (one per task, created and discarded faster than a human types), I was exhausting ~200GB of disk every week and ended up writing cleanup scripts to reverse-engineer, from what was left on disk, which containers and databases belonged to deleted worktrees. A pre-remove hook runs the same teardown with the config still present.

This is not about vetoing removal — pre-remove needs no power to countermand anything. It's about doing cleanup with full information instead of reconstructing it afterward.

One limit, which is why post hooks still matter: pre-remove only applies to git worktree remove. For manual deletion + git worktree prune, the tree is already gone, so pre-remove can't see it. The two are complementary.

Cheers / Pozdrawiam, Michal

-- Michal Papis phone. +48 603 751 266

Back to recent threads