git/list[1] front-page[2] threads[3] people[4] search[5] about
 

[PATCH v1 0/3] worktree: add post-worktree-add and post-worktree-remove hooks

From
Domen Kožar <domen@cachix.org>
Date
Jul 9, 2026, 23:36 UTC
Message-ID
<7c8b4673-37ac-45fa-ad8c-a1dc09afe5fe@mtasv.net>
Hi everyone,

I maintain devenv, a developer environment manager, and lately the workflow we see most is people letting AI coding agents loose on a repository, one linked worktree per task, created and discarded at a pace no human would type. Each of those worktrees expects a working environment: processes, sockets, and stateful services such as a database seeded from a dump.

Today there is no reliable trigger to set that up when a worktree appears: post-checkout does not fire for --no-checkout or --orphan and cannot be told apart from a plain checkout. Nothing at all fires when a worktree goes away, so stale databases and services pile up after "git worktree remove" or a manual rm followed by "git worktree prune". Wrapping the worktree commands only helps when every tool, human or agent, goes through the wrapper.

Patch 1 adds a post-worktree-add hook that fires after the working tree is fully set up. Patch 2 adds post-worktree-remove for "git worktree remove". Patch 3 extends it to "git worktree prune" so that manually deleted worktrees are also observed.

Two design points I would especially appreciate feedback on:
 * post-worktree-add runs after post-checkout and is skipped when
   post-checkout fails. An argument could be made that it should run
   whenever the worktree was created, regardless of the earlier
   hook's exit status, since tooling registering worktrees would
   otherwise miss one that does exist.
 * for entries pruned because their gitdir file points to a location
   that no longer exists, the hook receives the recorded path; when
   the path cannot be determined at all (missing or corrupt gitdir
   file) it receives an empty string.

Thanks, Domen

Domen Kožar (3):
  worktree: add post-worktree-add hook
  worktree: add post-worktree-remove hook
  worktree: run post-worktree-remove hook when pruning
 Documentation/githooks.adoc |  41 +++++++++++++
 builtin/worktree.c          |  73 ++++++++++++++++++-----
 t/t2400-worktree-add.sh     | 113 ++++++++++++++++++++++++++++++++++++
 t/t2401-worktree-prune.sh   |  88 ++++++++++++++++++++++++++++
 t/t2403-worktree-move.sh    |  44 ++++++++++++++
 worktree.c                  |   1 -
 worktree.h                  |   6 +-
 7 files changed, 347 insertions(+), 19 deletions(-)
base-commit: f85a7e662054a7b0d9070e432508831afa214b47
-- 
2.54.0
Next: Phillip Wood
Message 1 of 21 in “worktree: add post-worktree-add and post-worktree-remove hooks”
  1. 0/3 worktree: add post-worktree-add and post-worktree-remove hooksDomen Kožar, Jul 9, 2026
  2. Phillip WoodJul 10, 2026
  3. 0/4 worktree: add lifecycle hooksDomen Kožar, Aug 4, 2026
  4. Caleb WhiteAug 4, 2026
  5. Junio C HamanoAug 4, 2026
  6. Domen KožarAug 30, 2026
  7. Domen KožarSep 7, 2026
  8. Kristoffer HaugsbakkSep 7, 2026
  9. Domen KožarSep 7, 2026
  10. Maciej CiemborowiczOct 3, 2026
  11. 0/2 worktree: add post-worktree lifecycle hookDomen Kožar, Oct 4, 2026
  12. Junio C HamanoOct 5, 2026
  13. Phillip WoodJul 13, 2026
  14. 1/4 worktree: add post-worktree-add hookDomen Kožar, Aug 4, 2026
  15. Caleb WhiteAug 4, 2026
  16. 4/4 worktree: add post-worktree-move hookDomen Kožar, Aug 4, 2026
  17. 2/4 worktree: add post-worktree-remove hookDomen Kožar, Aug 4, 2026
  18. 3/4 worktree: run post-worktree-remove hook when pruningDomen Kožar, Aug 4, 2026
  19. 1/2 worktree: add post-worktree lifecycle hookDomen Kožar, Oct 4, 2026
  20. Phillip WoodOct 6, 2026
  21. 2/2 worktree: notify post-worktree hook when pruningDomen Kožar, Oct 4, 2026

Read the whole thread, see it on lore, or plain text.

$ cat FOOTERMessages come from the public archive at lore.kernel.org/git, fetched every hour. The front page is chosen and written each morning by an AI editor and can be wrong; the threads themselves are the record. About and API. For agents: an MCP server at https://gitlist.dev/mcp, and any thread, story or person page as Markdown by adding .md to its URL (or sending Accept: text/markdown). Details in /llms.txt.