Sub topic 2
How a worktree relates to its repository
This chapter assumes you are comfortable with everyday Git (commits, branches, merges, diffs) and know what a commit hash and a branch ref are. It explains what a linked worktree shares with the main repository and what it keeps to itself. The later chapters on setup and isolation depend on this model.
One repository, several working trees
A normal clone has one repository, stored in the .git directory, and one working tree, the checked-out files you edit. A linked worktree adds another working tree that uses the same repository. You get a second directory with its own files, its own checked-out branch and its own index, but no second copy of the history.
The original clone is the main worktree. Every other one is a linked worktree. Git treats them the same for day-to-day work: you edit, stage and commit in any of them.
What is shared and what is separate
Because the repository is shared, a commit made in one worktree is visible from all the others as soon as it exists. No fetch or push is needed between them.
| Shared by all worktrees | Separate for each worktree |
|---|---|
| Commit and file objects | The checked-out files |
| Branches and tags | `HEAD`, the current branch or commit |
| Repository config in `.git/config` | The index (staging area) |
| Hooks and remotes | In-progress state such as a merge or rebase |
| The stash | Untracked and ignored files |
Two rows deserve attention. The stash is shared, so a git stash in one worktree shows up in git stash list in every other worktree. Untracked files such as .env or node_modules exist only in the directory where they were created, so a new worktree starts without them.
Where the data lives
You can see the mechanism on disk. The main worktree keeps the full .git directory. A linked worktree has a .git entry that is a small file, not a directory. It holds a pointer back to the main repository.
git worktree add -b agent/auth ../myrepo-auth main
cat ../myrepo-auth/.git
ls .git/worktrees/The cat command prints a line like gitdir: /home/you/myrepo/.git/worktrees/myrepo-auth. That directory inside the main .git holds the per-worktree data: that worktree's HEAD, its index and a link back to its path. Git uses this record to know which worktrees exist and which branch each has checked out.
Why this matters for parallel agents
Each agent gets files and an index of its own, so one agent's edits and staging never touch another's. That removes the overwriting problem from a single shared checkout.
The shared parts create two consequences you should plan for:
- Commits are instantly visible everywhere, which makes reviewing an agent's branch from the main worktree easy.
- Anything stored in the shared repository can still collide: the stash, branch names, and repository-wide config or hooks. A later chapter covers these hazards.
Check your model
git worktree list
git -C ../myrepo-auth status
git -C ../myrepo-auth log --oneline -1git worktree list prints each worktree's path, current commit and branch. Running status against the linked worktree shows its own index and files, while log shows the same history the main worktree sees.
Exercise
You run git stash inside a linked worktree to set aside half-finished edits. Later you run git stash list in the main worktree. Will you see the stash entry, and why? Also, will the stashed edits disappear from the main worktree's files?
Show answer
Yes, the entry appears, because the stash lives in the shared repository and not in a single working tree. The main worktree's files are unaffected. Stashing only removes changes from the linked worktree where you ran it, since each worktree has its own files and index.