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 that a repository keeps its data in a .git directory. It explains what a linked worktree shares with the repository it came from and what it keeps for itself. Later chapters build on this, so it is worth getting the model right.
One repository, several working trees
A normal clone has two parts:
- The working tree is the set of files you edit.
- The repository data is everything under
.git: commits, file contents, branches, tags and configuration.
Usually there is one of each, so a repository has exactly one set of files on disk at a time. Switching branches rewrites those files.
A linked worktree is an additional working tree attached to the same repository. It has its own directory of files, its own checked-out branch and its own staging area. It does not get its own copy of the history. The checkout you originally cloned or initialized is called the main worktree. Git treats the main worktree and linked worktrees almost identically once they exist.
For parallel coding agents, this separation is what matters. Each agent edits files in its own directory, so no agent overwrites another's files. All agents still use one object store, which keeps disk use low and makes their commits visible to each other immediately.
What lives where
Git splits repository state into two groups. Data that describes the project is shared. Data that describes "what is checked out right now" is per-worktree.
| Item | Shared or per-worktree | Notes |
|---|---|---|
| Objects (commits, trees, file contents) | Shared | Stored once in the main `.git/objects` |
| Branches and tags (`refs/heads`, `refs/tags`) | Shared | A commit on a branch is visible from every worktree at once |
| Remote-tracking branches | Shared | A `git fetch` in one worktree updates all of them |
| `.git/config` | Shared | Settings such as remotes and `user.email` apply everywhere |
| Hooks | Shared | The same hook scripts run in every worktree |
| Stash | Shared | The stash is a single list across all worktrees |
| `HEAD` | Per-worktree | Each worktree has its own current branch or commit |
| Index (staging area) | Per-worktree | Staging in one worktree never affects another |
| Merge, rebase and cherry-pick state | Per-worktree | Each worktree can be mid-merge independently |
| Working files, including untracked and ignored ones | Per-worktree | Each worktree has its own copy of the files |
The last row matters in practice. Files that Git does not track, such as .env, node_modules or build output, exist only in the worktree where they were created. A new worktree starts with none of them.
How Git links the two directories
The linking is done with plain files, which you can inspect.
In the main worktree, .git is a directory. In a linked worktree, .git is a single file with one line pointing into the main repository's metadata:
gitdir: /home/you/src/myrepo/.git/worktrees/myrepo-authThat target directory, .git/worktrees/<name>/, is the private area for that worktree. It holds the worktree's HEAD and index, and a few small bookkeeping files:
gitdircontains the path back to the linked worktree's.gitfile.commondircontains a relative path to the shared.gitdirectory.lockedexists only if the worktree is locked, and holds the reason.
When you run a Git command inside a linked worktree, Git reads the .git file, finds the private directory for per-worktree state, and follows commondir to find the shared state. Both directions of the link are stored, so Git can also list every worktree from the main repository.
Here is a complete session you can run to see this. It uses a throwaway repository, so it is safe to try.
git init -b main demo
cd demo
git commit --allow-empty -m "initial commit"
git worktree add -b agent/auth ../demo-auth main
# The linked worktree's .git is a file, not a directory
cat ../demo-auth/.git
# The private metadata directory in the main repository
ls .git/worktrees/demo-auth
# Which directory holds per-worktree state, and which holds shared state
git -C ../demo-auth rev-parse --git-dir --git-common-dirThe cat command prints a gitdir: line like the one above. The ls command lists files such as HEAD, commondir and gitdir. The final command prints two paths: the first (--git-dir) points into .git/worktrees/demo-auth, and the second (--git-common-dir) points to the main .git. Depending on your Git version, the paths may be absolute or relative.
git rev-parse --git-dir and --git-common-dir are the two commands to remember. In the main worktree they return the same directory. In a linked worktree they differ, and the difference is the boundary between per-worktree and shared state.
Seeing sharing in action
Because refs and objects are shared, a commit made in one worktree is visible from the others without any push or fetch. Continue the session above:
# Make a commit inside the linked worktree
cd ../demo-auth
echo "login form" > login.txt
git add login.txt
git commit -m "Add login form"
# Back in the main worktree: the commit is already there
cd ../demo
git log --oneline agent/auth
# But the file is not in the main worktree's files
ls login.txtThe git log command shows the new commit on agent/auth. The ls command fails with "No such file or directory". The commit is in the shared repository, but the main worktree has main checked out, and main does not contain that commit. The two directories show two different states of the same repository.
This is the property you rely on when reviewing agent work. You can inspect, diff and merge an agent's branch from any worktree while the agent is still running.
What the model means for agents
Three consequences follow directly from the table above.
- File edits are isolated. Two agents in two worktrees cannot overwrite each other's files, and
git statusin each worktree reports only that agent's changes. - Staging and history operations are isolated. An agent can run
git add,git commit,git rebaseorgit resetin its worktree without disturbing another agent's index. - Repository-wide state is not isolated. Branch names, tags, configuration, hooks and the stash are common to all agents. If one agent deletes a branch, edits
.git/configor runsgit stash, every other worktree sees the effect.
The third point is the source of most surprises with parallel agents. A later chapter covers these hazards in detail. For now, remember that a worktree isolates files, not the whole repository.
Edge cases to know
- Moving directories breaks the links. The main repository stores the absolute path to each linked worktree, and each linked worktree stores the path back. If you move either one with
mv, the link breaks. Git providesgit worktree repairto re-establish the links, andgit worktree moveto relocate a linked worktree correctly. - Deleting a worktree directory by hand leaves metadata behind. The entry under
.git/worktrees/stays until Git prunes it. A later chapter covers safe removal. - A branch can be checked out in one worktree only. Git enforces this to prevent two working trees from advancing the same branch out from under each other. The next chapters cover this rule and the detached HEAD workaround.
- Per-worktree configuration is opt-in. By default every worktree reads the same
.git/config. If you need a setting to differ per worktree, Git supportsgit config --worktreeafter you enable theextensions.worktreeConfigsetting. - Submodules have limited support in linked worktrees. The Git documentation describes multiple checkouts of a superproject as experimental. If your project uses submodules, test the workflow before relying on it.
Exercise
You run git fetch origin inside a linked worktree, then switch to the main worktree and run git log origin/main. Do you see the newly fetched commits? Then explain why running git add . in the linked worktree does not change what git status shows in the main worktree.
Show answer
Yes. Remote-tracking branches are refs, and refs are shared across all worktrees, so the fetch updates them for every worktree. git add . does not affect the main worktree because the index (staging area) is per-worktree. Each worktree keeps its own index under .git/worktrees/<name>/, and the main worktree uses the index in the main .git directory.
Exercise
In a linked worktree, git rev-parse --git-dir prints /home/you/src/myrepo/.git/worktrees/myrepo-search. What does git rev-parse --git-common-dir print there, and which of the two would hold the object store?
Show answer
It prints /home/you/src/myrepo/.git (possibly as a relative path). The common directory holds the object store, because objects are shared. The per-worktree directory under .git/worktrees/ holds only that worktree's HEAD, index and bookkeeping files.