About

git worktree Is Neither a Symlink Nor a Clone — One .git, Several Working Folders

Running several Claude Code sessions? Give each one its own worktree

I used worktrees for months without being sure what they were. A symlink? A full copy like a clone? I finally opened one up. It's neither. The working files are a real copy, and the repository (.git) is shared. Once that clicks, splitting several Claude Code sessions so they don't trample each other is obvious.

.git is a file, not a folder

I ran git worktree add ../repo-feature-x -b feature-x and looked at .git in the new folder.

$ cat ../repo-feature-x/.git
gitdir: /path/to/repo/.git/worktrees/repo-feature-x
$ file ../repo-feature-x/.git
../repo-feature-x/.git: ASCII text

Not a link. A one-line text file. Git reads that line and goes to .git/worktrees/repo-feature-x/ in the main repository, where this worktree keeps its own HEAD, index and gitdir.

~/repo main worktree .git/ the one real repository objects/ shared refs/ (branches, tags, stash) shared worktrees/feature-x/ HEAD · index · gitdir per worktree app/ ... checked-out files ~/repo-feature-x linked worktree .git a text file, not a folder gitdir: ~/repo/.git/ worktrees/feature-x no symlink, no second .git/ app/ ... a real second copy

The rule in the official docs (git help worktree, DETAILS and REFS) is short: pseudo refs like HEAD are per worktree, everything under refs/ (branches, tags, stash) is shared. A commit in one worktree shows up in git log feature-x in the other right away, and one fetch serves them all.

Symlink, worktree and clone side by side

Symlink worktree clone
Working files same files separate copy separate copy
objects · refs - shared separate
HEAD · index - separate separate
Commit visible in the other - instantly after push/fetch

Measured on this repository (2026-10-19, du -sh): 461MB of working files, 438MB of .git. Another worktree adds 461MB. Another clone adds close to 900MB because it brings its own .git.

One branch, one place

$ git worktree add ../dup feature-x
fatal: 'feature-x' is already checked out at '.../repo-feature-x'

I first read this as an annoying limit. With parallel sessions it turns out to be a guard: git stops two sessions from editing the same branch before it happens.

If you delete the folder with rm -rf, the record stays in .git/worktrees/. git worktree list marks it prunable, and the branch still counts as checked out, so you can't open it elsewhere. One git worktree prune clears it. Better to delete with git worktree remove in the first place.

One worktree per Claude Code session

When several Claude Code sessions share one working folder, real things break. They overwrite each other's uncommitted changes. One runs git checkout and the other's work lands on the wrong branch.

So the rule is simple: one task, one worktree. They live inside the repo at .claude/worktrees/, which is in .gitignore. Claude Code's EnterWorktree creates them in the same place, so the ones I open and the ones an agent opens sit together. This post was written in one of those worktrees while five sessions edited five blogs at once.

For opening one by hand I keep a function that creates the worktree if it's missing and just cds otherwise. New tab, wt 1, claude, done.

wt() {  # wt 1 → <repo>/.claude/worktrees/wt-1
  local top dir
  top=$(dirname "$(git rev-parse --path-format=absolute --git-common-dir)")
  dir="$top/.claude/worktrees/wt-$1"
  if [[ ! -d $dir ]]; then
    git -C "$top" worktree prune
    git -C "$top" fetch -q origin main
    git -C "$top" worktree add -q --no-track -b "wt/$1" "$dir" origin/main
    cp "$top/.env" "$dir/" 2>/dev/null || true
  fi
  cd "$dir"
}

--git-common-dir finds the main repository, so calling it from inside a worktree lands in the same place. --no-track keeps the new branch from taking origin/main as its upstream. Rename it with git branch -m once the task has a name.

Two things that don't come along

A new worktree only checks out tracked files. No .env, no config/master.key, no node_modules/, no SQLite database. That's why the function copies .env.

The stash is shared, because refs/stash lives under refs/. One session's git stash pop can pop another session's stash. Inside worktrees I park changes in a temporary WIP commit instead.

Worktree or clone?

Editing code and opening a PR: a worktree is enough. Running a dev server to look at the screen: keep a separate clone. Ports, database and node_modules are independent from the start, and docker-compose usually mounts only the main checkout.

If you're wiring several agents into one repository and can't tell where they collide, feel free to ask us at https://www.negabaro.com/contact.

How It Works

1

`git worktree add ../path -b branch` -> checks out files into a new folder and writes a one-line gitdir file as its .git

2

Git follows that line to HEAD and index in the main .git/worktrees/<name>/

3

objects and refs (branches, stash) are shared, so commits show up immediately

4

Clean up with `git worktree remove ../path`. If you used rm -rf, run `git worktree prune`

Pros

  • ✓ Less disk than a clone, one fetch for all
  • ✓ Git refuses to check out the same branch twice

Cons

  • ✗ .env, master.key and node_modules do not come along
  • ✗ The stash is shared; you can pop another session's stash

Use Cases

Isolating a workspace per Claude Code or other agent session Dedicated checkouts for reviewing PRs Working on another branch while a long build runs
← Using git stash Properly What Is Portless? How It Replaces localhost Port Numbers with Names →