Git worktrees let me keep two branches of the same repo open in different folders. That’s handy when I want to review a change without stopping what I’m working on. The part I didn’t expect was how quickly those extra folders could fill a disk.
Git already shares the repository history between worktrees. What it doesn’t share is everything you install or build afterward: node_modules, .next, and other generated files. My first question was: if the lockfile is the same, why install the same packages twice?
Start with the package manager the repo already uses. Bun already shares much of the storage on a Mac. For an npm repo, I’d install only where I need to run code, then consider cloning a known-good install if that’s still eating disk.
If the repo uses Bun
Run this from the new worktree’s repo root:
bun install --frozen-lockfile
Bun keeps downloaded packages in a shared cache. On macOS, when the cache and worktree are on the same APFS volume, it normally uses copy-on-write clones. Think of it as two folders with separate files that point to the same disk data until one copy changes. Each worktree still has its own node_modules, and Bun can reuse unchanged packages even when the branches have different lockfiles. How Bun’s cache works
Freezing the lockfile doesn’t save space. I use the flag when I’m setting up an existing branch and don’t want an install to change the dependencies it recorded. If an edit would require a lockfile update, the install stops. If I’m intentionally changing a dependency, I use bun add or bun update and commit the updated lockfile. Bun’s install options
If you only use bun run but install with npm, you’re on the npm path below.
If the repo uses npm
For a worktree where I’m only reading code, I skip the install. When I need to run tests or start the app, I use:
npm ci --prefer-offline
That keeps the repo’s package-lock.json and prefers downloads npm already cached. It still writes a separate installed tree, so it helps with setup time more than disk space. npm ci · npm cache settings
Reuse an npm install when it matches
If several worktrees need to stay runnable on the same Mac, Worktrunk’s copy-ignored command can clone installed files using APFS. In your primary worktree (the source), put node_modules/ in a .worktreeinclude file at the root. Keep .env and unrelated caches off that list. Then, from the new worktree on the same volume, preview what would be copied:
wt step copy-ignored --require-include --dry-run
Remove --dry-run only if the preview is right and the source has a known-good install. Check the lockfile, workspace manifests, Node/npm versions, and install settings. If a dependency comes from a local folder, or a setup script generates files for a specific checkout, a fresh install is the simpler choice. Run the repo’s tests from the new worktree before making this your default. Don’t follow a successful clone with npm ci: it replaces the cloned tree.
Why not just symlink the entire folder? Suppose two branches have the same lockfile, but the new branch edits packages/ui. npm links that workspace package inside node_modules. If both branches point at the first branch’s node_modules, tests in the second can silently use the first branch’s packages/ui. Separate installs or correctly cloned trees keep that link local. How npm links workspaces
Measure what you’re actually saving
du -sh node_modules is useful for spotting large folders, but it doesn’t tell you how much extra space an APFS clone used. Compare the free space on the volume before and after a test install with df -h .. If dependency installs aren’t the main culprit, check build outputs and old worktrees before changing package managers. How APFS clones work
One optional step for Bun users: Bun 1.4’s global virtual store links eligible installed packages from a shared location and keeps workspace packages local. It requires the isolated linker, which can expose dependencies a repo forgot to declare. I’d try it on one repo only if normal Bun installs still feel slow.