# Deploying from a clean git worktree: only pushed code goes live

> The deploy used to build inside the live site folder, so files another AI session was half-way through editing got built too. Now each deploy checks out origin/main into a git worktree and builds there.

- Date: 2026-11-29
- Topics: DevOps, Next.js
- Chinese original: https://kbs0830.com/blog/deploy-from-clean-worktree

An earlier post, "Several AI sessions in one folder" (in Chinese), left one problem unsolved: the deploy script runs `git pull` in the live site folder and then goes straight to `next build`, so **the build reads the files in the folder, not the version in git**. Files another session is half-way through editing, not yet committed, get built too. A failed build is the small problem; if the build succeeds, it goes live, and git does not even contain that code.

## Why not "build elsewhere and move it back"

The obvious approach is to build in a temporary folder and move the output back into the live folder. But a Next.js build output has the absolute path of where it was built baked in, so if you move it, the server looks for files in the wrong place.

So I did it the other way round: **let the site run directly from a clean copy**.

## The approach: `.live` is a git worktree

```powershell
git fetch origin main
git worktree add --detach .live origin/main      # the first time
git -C .live checkout -f --detach origin/main    # every time after
```

`.live` sits inside the live site folder (added to `.gitignore`) and contains only `origin/main`, meaning only what has really been pushed. Build output goes in `.live/.next-a` and `.live/.next-b`, two slots used in turns, as before.

Putting it there has one advantage: **`.live` has no `node_modules` of its own, and Node looks one level up and finds the one in the live site folder**, so there is no reinstalling packages on every deploy. Only Turbopack needs extra handling: it only looks inside "the level of the nearest lockfile" and reports `Could not find the Next.js package`. In `next.config.ts` I just pull the root up to the level that has `node_modules`:

```ts
function findWorktreeRoot(dir: string) {
  if (existsSync(path.join(dir, "node_modules"))) return undefined;
  for (let d = path.dirname(dir); d !== path.dirname(d); d = path.dirname(d)) {
    if (existsSync(path.join(d, "node_modules", "next", "package.json"))) return d;
  }
}
const root = findWorktreeRoot(process.cwd());
// nextConfig: ...(root && { turbopack: { root }, outputFileTracingRoot: root })
```

## Code in `.live`, data stays where it was

Visitor stats, the admin database and logs are not in git, so `.live` does not have them. The server start script is split across two directories:

- **appDir (`.live`)**: the Next.js code, posts, architecture diagrams, and even the `git log` that `/changelog` reads
- **projectDir (the live site folder)**: `data/`, `logs/`, drafts, `.env.local`

The pointer file that says which slot is live changed from `.next-a` to `.live/.next-a`; when the server sees a path in it, it treats the first part as appDir. The old format is still understood, so if the health check fails the first time after switching over, the fallback is the old slot in the original folder.

The scheduled task still runs the very same `scripts/server.js`, with **not a single line changed**, so no administrator permission is needed and the site did not have to go down for maintenance.

## A dry run

I could not try it on the live site, so I first created `.live` by hand, built into `.live/.next-test`, started it on a different port with the real start script, and ran the full E2E: all 171 tests passed. The data on the pages (admin to-dos, drafts, radar) all loaded, and no stray `data/` folder appeared inside `.live`.

## One more small trap

Git's pre-commit hook runs `node node_modules/typescript/bin/tsc`, which cannot be found inside a worktree. After a successful deploy, the script automatically commits the regenerated resume PDF, and that step runs inside `.live`, so the hook failed. I changed it to look for `node_modules` in parent directories:

```sh
root="$PWD"
while [ ! -d "$root/node_modules/typescript" ] && [ "$root" != "$(dirname "$root")" ]; do root="$(dirname "$root")"; done
node "$root/node_modules/typescript/bin/tsc" --noEmit
```

The session worktrees of Claude Code are in the same situation, so that is solved along with it.

---

Author: PINGWEI LI (李秉威, Li Ping-Wei)
Original page: https://kbs0830.com/en/blog/deploy-from-clean-worktree
