﻿---
title: "Git worktree 里的项目钩子，看主仓 .codex 而不是这棵树"
summary: "链接 worktree 的 .git 是文件。项目 hooks.json 以主 checkout 为准。发现失败就改用 ~/.codex/hooks.json。"
category: hooks
level: advanced
surfaces: [cli, app]
tags: ["hooks", "worktree", "hooks.json"]
canonical: /tips/worktree-hooks-use-root-codex/
---

# Git worktree 里的项目钩子，看主仓 .codex 而不是这棵树

链接 worktree 的 .git 是文件。项目 hooks.json 以主 checkout 为准。发现失败就改用 ~/.codex/hooks.json。

链接 worktree 里 `.git` 是指向 `gitdir:` 的文件，不是目录。Codex 对项目钩子的权威路径是**主仓**的 `.codex/`（`hooks.json` 和 TOML 钩子），好让同一仓库共用一份信任哈希。

所以：团队门禁用的 `PreToolUse` / `Stop` 写进主仓 `.codex/hooks.json` 并提交。不要只在 `$CODEX_HOME/worktrees/...` 里放一份，以为这棵树会单独执行。

如果 `codex doctor` 认得出项目，但只有用户级钩子在跑：

```bash
# 确认当前是不是链接 worktree
git rev-parse --is-inside-work-tree
git rev-parse --show-toplevel
git rev-parse --git-common-dir
```

`--dangerously-bypass-hook-trust` 解决不了「根本没扫到文件」。临时绕过：把同一份钩子放到 `~/.codex/hooks.json`。桌面托管 worktree 另走 Local environment setup，和 CLI 这套发现不是同一条路。

## 来源

- [openai/codex#27133](https://github.com/openai/codex/issues/27133)
- [OpenAI · Hooks](https://learn.chatgpt.com/docs/hooks)
