Published on

TIL: git worktrees run hooks from the main checkout

TIL: git worktrees run hooks from the main checkout
Authors

I changed a hook in a worktree, committed, and the old hook ran anyway. Same behavior, as if I'd never touched it.

Here's why. Git keeps hooks in the common git directory, and every worktree shares it. The tracked copy in your branch (for example .githooks/pre-push) is just a file in the working tree. It does nothing until something installs it into .git/hooks.

You can ask git where it looks, right from inside the worktree:

git rev-parse --git-path hooks
# /path/to/main-checkout/.git/hooks

To prove it, I put a hook in the main checkout's .git/hooks/pre-commit that prints a line, then committed inside a separate worktree. The line printed.

The fix

An edit to the tracked hook doesn't apply until the installed copy is updated. You've got two options:

  • Re-run whatever installs the hooks after you change them.
  • Set core.hooksPath to the tracked folder, so git reads the files you edit.

Next time a hook seems to ignore your change, check the installed copy before the tracked one.