Create a Git worktree next to your current repo for a branch, copy useful local files, install deps, and open it in your editor.
Running worktree-add <branch> from inside a repo:
- Normalizes
<branch>(supportsorigin/foo,refs/heads/foo, etc.). - Refuses if that branch is already checked out in any worktree.
- Picks a destination next to your current repo:
../<repo>-<safe-branch>. - If the destination exists, asks before moving it to the system trash.
- Fetches
origin/<branch>when needed and creates a git worktree:- reuses an existing local branch
- or creates a tracking branch from
origin/<branch> - or creates a new branch from the current
HEAD
- Copies untracked / ignored files into the new worktree, skipping heavy stuff
(
node_modules,dist,.next, caches, virtualenvs, etc.). - Detects your package manager and installs dependencies with lockfile‑safe flags
(
npm ci,pnpm install --frozen-lockfile,yarn install --immutable, etc.). - If the project uses Next.js and supports it, runs
next typegen. - Opens the new worktree in your editor.
Your original checkout is left untouched.
- Node.js ≥ 22.14.0
- Git with
git worktreesupport
You usually don’t need a global install.
# inside /my/path/my-app
# one‑off
npx worktree-add feature/my-branch
# or install globally
pnpm add -g worktree-add # or: npm i -g worktree-add
worktree-add feature/my-branchRun it from anywhere inside an existing worktree of the repo.
# inside /my/path/my-app
worktree-add <branch>Run this inside an existing worktree of your project—the tool discovers the repo root from your current directory and creates the sibling worktree next to it.
Example:
# inside /my/path/my-app
# reuses a local branch, tracks origin/<branch> if it exists, otherwise creates a new branch from current HEAD
worktree-add feature/login-formA new branch from the current HEAD is created only when the branch does not already
exist locally or on origin/.
Destination directory (assuming repo named my-app):
/my/path/my-app # current checkout
/my/path/my-app-feature-login-form # new worktree for branch "feature/login-form"
By default the tool opens the new worktree in:
--editor <command>if passedWORKTREE_ADD_EDITORenv var- otherwise
code
Only simple command names are allowed (no ;, &, pipes, etc.) to avoid shell injection. Examples:
worktree-add feature/foo -e code
WORKTREE_ADD_EDITOR=vim worktree-add feature/fooIf launching the editor fails, the worktree still stays created and ready.
Tip: add a shell helper with your preferred editor in your shell profile. Example snippet to add:
worktree-add() { WORKTREE_ADD_EDITOR=cursor command worktree-add "$@" }Add it to your shell profile:
- zsh:
~/.zshrcor~/.zprofile - bash:
~/.bashrcor~/.bash_profile - fish:
~/.config/fish/config.fish
MIT