Git worktree command reference: path naming, create/remove/prune, `.env*` copy, package-manager install, and cleanup. Use when implementing or debugging worktree setup — not as the primary user-facing workflow (see nv-worktree-create).
79
100%
Does it follow best practices?
Run evals on this skill
Adds up to 20 points to the overall score
View guide
Passed
No findings from the security scan
Reference for agents implementing worktree workflows. Prefer nv-worktree-create for the end-to-end flow.
REPO_ROOT = $(git rev-parse --show-toplevel)
REPO_PARENT = $(dirname "$REPO_ROOT")
BRANCH = user-provided name (used as git branch)
DIR_NAME = sanitize(BRANCH) # lowercase; `/` → `-`
WORKTREE = "$REPO_PARENT/$DIR_NAME"Examples (repo at /Users/me/work/novu):
| Branch | Worktree path |
|---|---|
more-dcr-oauths | /Users/me/work/more-dcr-oauths |
nv-1234/foo-bar | /Users/me/work/nv-1234-foo-bar |
Abort if WORKTREE already exists on disk or in git worktree list.
From current HEAD:
git worktree add -b <branch> <worktree-path>From a specific base:
git worktree add -b <branch> <worktree-path> <base-ref><base-ref>: HEAD, main, origin/main, a tag, or a commit SHA.
git worktree list
git -C <worktree-path> status -sb
git -C <worktree-path> rev-parse HEADgit worktree add does not bring gitignored files. Copy local secret env files only from the main checkout — not .env.example / .env.template (those are already in git).
Preferred — rsync with explicit includes (from main repo root):
rsync -a \
--prune-empty-dirs \
--include='*/' \
--include='.env' \
--include='.env.*' \
--exclude='.env.example' \
--exclude='.env.template' \
--exclude='*' \
--exclude='node_modules/' \
--exclude='dist/' \
--exclude='build/' \
--exclude='.next/' \
--exclude='.turbo/' \
"<main-repo-root>/" "<worktree-path>/"Alternative — copy only gitignored env files (when rsync is unavailable):
cd <main-repo-root>
git ls-files -z --others --ignored --exclude-standard \
| while IFS= read -r -d '' f; do
base="$(basename "$f")"
case "$base" in
.env|.env.*)
case "$base" in
.env.example|.env.template) continue ;;
esac
mkdir -p "<worktree-path>/$(dirname "$f")"
cp -a "$f" "<worktree-path>/$f"
;;
esac
doneAfter copying, list what landed (e.g. find <worktree-path> -type f \( -name '.env' -o -name '.env.*' \) ! -name '.env.example' ! -name '.env.template').
node scripts/setup-env-files.js. That script generates a new STORE_ENCRYPTION_KEY when .env is missing — a worktree would then disagree with the parent and shared local Mongo/Redis.apps/api/src/.env, .env.agent, .env.development, apps/dashboard/.env, apps/worker/src/.env, apps/ws/src/.env, etc.playground/ env unless the task needs it.| Lockfile | Command |
|---|---|
pnpm-lock.yaml | pnpm install |
yarn.lock | yarn install |
package-lock.json | npm install |
bun.lockb | bun install |
Run from the worktree root.
pnpm build # or npm/yarn/bun equivalentSkip if no build script exists.
git worktree remove <worktree-path>
git worktree pruneRemove uncommitted work in the worktree first, or use --force only when the user explicitly asks.
For auditing merged/stale worktrees and batch cleanup, read nv-worktree-cleanup.
git worktree add --detach when the goal is a named feature branch — use -b.cp -r the entire repo instead of git worktree add.~/.cursor/worktrees/… detached paths when a sibling ../<name> worktree is enough.pnpm setup:agent) in Cursor Cloud child worktrees when the parent already ran them.d921755
If you maintain this skill, you can claim it as your own. Once claimed, you can manage eval scenarios, bundle related skills, attach documentation or rules, and ensure cross-agent compatibility.