﻿---
title: "worktree 不会自动带上 .env，用 .worktreeinclude"
summary: "托管 worktree 从 Git checkout 起步。被 ignore 的本地文件要在仓库根列进 .worktreeinclude，生产密钥不要这么拷。"
category: cloud
level: intermediate
surfaces: [app, cli]
tags: ["worktree", ".env", "桌面"]
canonical: /tips/worktreeinclude-ignored-files/
---

# worktree 不会自动带上 .env，用 .worktreeinclude

托管 worktree 从 Git checkout 起步。被 ignore 的本地文件要在仓库根列进 .worktreeinclude，生产密钥不要这么拷。

桌面应用创建的托管 worktree 只有已跟踪文件。`.gitignore` 里的 `.env.local`、`node_modules` 默认不在。

需要的 ignore 文件，在仓库根放 `.worktreeinclude`：

```gitignore
.env
.env.local
```

官方会自动拷一份被 ignore 的 `AGENTS.override.md`。这只作用于本机 ChatGPT 桌面应用托管的 worktree，不适用于远程树，也不适用于你自己 `git worktree add` 出来的树。

生产密钥不要靠拷贝。更稳的做法：

- 跟踪一份无密钥的 `.env.test`
- 测试用假变量或依赖注入，避免构造客户端时直接读生产环境
- 第一轮先检查 worktree 里有没有 `node_modules` 和环境文件；工具创建的树可能没跑本地 environment setup

`Handoff` 同样不会带走 ignore 文件，除非它们在 `.worktreeinclude` 里。

## 来源

- [OpenAI · Worktrees](https://learn.chatgpt.com/docs/environments/git-worktrees)
- [OpenAI Community · worktree .env preflight](https://community.openai.com/t/codex-worktrees-need-a-runtime-preflight-for-missing-env-files/1393774)
