Sub topic 6
What worktrees share and what they keep separate
This chapter assumes you can commit, branch and merge, and that you know the difference between the working directory, the index and the repository history. It also assumes you have made at least one linked worktree with git worktree add. Here you learn exactly which pieces of repository state live in one place for everyone, and which pieces each worktree owns. That knowledge explains most of the surprises people hit with worktrees.
The core idea: one repository, several checkouts
A linked worktree is not a copy of the repository. It is an extra working directory that points at the same repository. The commits, branches, tags and configuration live once. What each worktree adds is the small set of files that describes one checkout: which commit or branch is checked out, what is staged, and the files on disk.
This is the main difference from a second clone. A second clone duplicates the whole history and keeps its own branches. A commit made in one clone does not exist in the other until you push and fetch. With linked worktrees, a commit made in one is visible to all of them immediately, because there is only one object database.
Seeing the split with two commands
Git exposes the split directly. Run these inside a linked worktree:
git rev-parse --git-dir
git rev-parse --git-common-dirIn a linked worktree the first command prints a path such as /home/you/myproject/.git/worktrees/myproject-hotfix. This is the private administrative directory for that worktree. The second prints /home/you/myproject/.git, the shared repository directory.
In the main worktree both commands print the same .git directory, because the main worktree's private files live directly in it.
You can also see how the link is stored. In a linked worktree, .git is a file, not a directory:
cat ../myproject-hotfix/.gitgitdir: /home/you/myproject/.git/worktrees/myproject-hotfixGit reads that line and finds its private directory. The private directory in turn contains a gitdir file pointing back at the worktree's .git file. This two-way link is what git worktree repair restores after a manual move (covered in a later chapter).
What is shared
All worktrees of a repository see the same copy of the following.
| Shared item | Where it lives | What it means in practice |
|---|---|---|
| Object database | `.git/objects` | A commit made in any worktree is available in all of them at once. |
| Branches and tags | `.git/refs/heads`, `.git/refs/tags`, `packed-refs` | Creating, moving or deleting a branch in one worktree affects the others. |
| Remote-tracking branches | `.git/refs/remotes` | One `git fetch` updates `origin/*` for every worktree. |
| Configuration | `.git/config` | Remotes, user settings and aliases set at repository level apply everywhere. |
| Hooks | `.git/hooks` | The same hook scripts run in every worktree, unless `core.hooksPath` says otherwise. |
| Stash | `refs/stash` | There is one stash list. A stash saved in one worktree shows in the others. |
| Reflog of branches | `.git/logs/refs` | Branch history is recorded once. |
Two consequences are worth stating plainly.
First, you only need to fetch once. After git fetch origin in any worktree, origin/main is up to date in all of them.
Second, branch changes are global. If you delete a branch with git branch -D old-work in one worktree, it is gone for every worktree. Git protects you from deleting a branch that is currently checked out in any worktree, but it will not stop you from deleting branches that nobody has checked out.
What is per worktree
Each linked worktree keeps its own copy of the state that describes one checkout.
| Per-worktree item | Where it lives | What it means in practice |
|---|---|---|
| `HEAD` | `.git/worktrees/<name>/HEAD` | Each worktree is on its own branch or at its own detached commit. |
| Index (staging area) | `.git/worktrees/<name>/index` | What you stage in one worktree does not appear in another. |
| Working files | The worktree directory | Edits, untracked files and build output are local to that directory. |
| In-progress operation state | `MERGE_HEAD`, `rebase-merge/`, `rebase-apply/`, `CHERRY_PICK_HEAD`, `BISECT_*` | A merge or rebase in one worktree does not block work in another. |
| Sparse-checkout and worktree config | `config.worktree`, `info/sparse-checkout` | These can differ per worktree when enabled (see below). |
The in-progress state deserves attention. In a single directory, a half-finished rebase blocks everything else you want to do. With worktrees, you can leave a rebase waiting for conflict resolution in one directory and keep working in another. Each one tracks its own rebase-merge/ directory inside its private administrative directory.
Per-worktree refs follow the same rule. Special references such as HEAD, ORIG_HEAD, MERGE_HEAD and the refs/bisect/ namespace are private, while refs/heads/ and refs/tags/ are shared. This is also why git bisect can run in one worktree while you work normally in another.
Proving it yourself
A short experiment makes the split concrete. Start from a repository named myproject with a main branch.
cd myproject
git worktree add ../myproject-demo -b demo main
cd ../myproject-demo
# Per-worktree: the index
echo "scratch" > notes.txt
git add notes.txt
git status --short # A notes.txt
cd ../myproject
git status --short # empty: nothing is staged here
# Shared: branches and commits
cd ../myproject-demo
git commit -m "Add notes"
cd ../myproject
git log --oneline demo -1 # shows "Add notes"
git branch --list # lists demoThe staged file appears only in myproject-demo. After the commit, the main worktree can see the new commit through the shared demo branch, even though it never touched that branch. Its own working files are unchanged, because it still has main checked out.
Per-worktree configuration
Most settings in .git/config apply to all worktrees. Sometimes you want a setting to differ. For example, a worktree used for a sparse checkout needs core.sparseCheckout enabled without changing the others.
Git supports this with an extension. Enable it once for the repository:
git config extensions.worktreeConfig trueAfter that, git config --worktree <key> <value> writes to config.worktree in the current worktree's private directory, and it applies only there.
git config --worktree core.sparseCheckout true
git config --worktree user.email "[email protected]"There is one trap. Once extensions.worktreeConfig is on, some settings that older Git versions put in the shared config, notably core.bare and core.worktree, should not be in the shared file. Git 2.20 and later moves these for you when you enable the extension through git worktree-aware tooling, but if you edit the config by hand, check that core.bare is not set to true in the shared file. If it is, linked worktrees may behave as if they were bare.
The branch rule follows from shared refs
The rule that a branch can be checked out in only one worktree comes from this split. A branch is a shared ref, and HEAD is private. If two worktrees had the same branch checked out, a commit in one would move the branch pointer, and the other worktree's index and files would then disagree with where HEAD points. Git would have no safe way to tell you that. So git worktree add and git switch refuse, and name the worktree that holds the branch. A detached HEAD avoids the rule because it is a private commit ID and not a shared ref.
Hooks, submodules and other edge cases
Hooks are shared, but they run with the current worktree as the working directory. A hook that assumes it runs from the main checkout can misbehave. Scripts should ask Git where they are:
#!/bin/sh
top=$(git rev-parse --show-toplevel)
cd "$top" || exit 1
echo "Running checks in $top"Submodules are the weak spot. Each worktree needs its own initialised submodules, and the submodule repositories are stored under .git/worktrees/<name>/modules for linked worktrees. Git's own documentation marks multiple checkout with submodules as experimental. If your project uses submodules, run git submodule update --init in each new worktree and avoid relying on removing such worktrees with plain git worktree remove, which refuses on a worktree containing submodules unless you pass --force.
Garbage collection is aware of worktrees. git gc treats each worktree's HEAD and index as roots, so commits that are only reachable from a detached worktree are kept while that worktree exists. Once you remove the worktree, those commits can be collected after the reflog expires. If you made commits at a detached HEAD that you want to keep, give them a branch before you remove the worktree.
Common errors and how to read them
| Symptom | Likely cause | Fix |
|---|---|---|
| `fatal: 'feature/search' is already checked out at '/path/other'` | The branch is checked out in another worktree. | Use a different branch, use `--detach`, or switch the other worktree first. |
| A file you staged is missing from another worktree's `git status` | The index is per worktree. | Stage in the worktree where you intend to commit. |
| A branch you expected has vanished | Branches are shared, and someone deleted it from another worktree. | Recover it from the reflog with `git reflog`, then `git branch name <sha>`. |
| A hook fails only in linked worktrees | The hook assumes the main worktree path. | Use `git rev-parse --show-toplevel` instead of fixed paths. |
| `git config` change appears everywhere | The default scope is the shared config. | Enable `extensions.worktreeConfig` and use `--worktree`. |
Summary
Objects, branches, tags, remotes, hooks, the stash and ordinary configuration are shared. HEAD, the index, working files, and the state of merges, rebases and bisects are private to each worktree. git rev-parse --git-dir and --git-common-dir show you the two locations. Keeping this model in mind lets you predict what a command in one worktree does to the others.
Exercise
You run git worktree add ../wt-a -b topic main, then in ../wt-a you edit app.py, run git add app.py and git commit -m "Update app". Back in your main worktree, which of the following can you see without any fetch or push: the new commit, the topic branch, the staged state of app.py? Explain, and say how you would confirm which directory holds the private state.
Show answer
You can see the new commit and the topic branch, for example with git log topic and git branch --list. Both live in the shared repository directory, so they are visible immediately. The staged state of app.py is not visible in the main worktree, and after the commit nothing remains staged anyway. The index is private to each worktree. To confirm the locations, run git rev-parse --git-dir in ../wt-a, which prints a path under .git/worktrees/, and git rev-parse --git-common-dir, which prints the shared .git directory.
Exercise
A teammate says: "I deleted the branch old-spike in my review worktree, so it is only gone there." Is that correct? What would you check before deleting a branch, and how could you recover it?
Show answer
It is not correct. Branches are refs stored in the shared repository directory, so deleting old-spike removes it for every worktree. Git refuses to delete a branch that is checked out in any worktree, so run git worktree list to see which branches are in use. To recover a deleted branch, find its last commit with git reflog (or the output Git printed when it deleted the branch) and recreate it with git branch old-spike <sha>.