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 created at least one linked worktree with git worktree add.
The working model
A linked worktree is not a second clone. It does not copy history. Instead, Git splits the repository's state into two groups:
- State that belongs to the repository as a whole and is shared by every worktree.
- State that belongs to one worktree and is kept in a private administrative directory.
Knowing which group a piece of state falls into explains most surprises you will meet.
What is shared
All worktrees of a repository use the same object database and the same references, with the exceptions listed in the next section. In practice this means:
- Commits, trees and blobs. A commit made in one worktree is stored once. Every other worktree can see it immediately.
- Branches and tags.
refs/headsandrefs/tagsare shared. If you create a branch in one worktree,git branchin another lists it. - Remotes and fetched data. Remote-tracking branches are shared. One
git fetchupdates them for every worktree. - Configuration. The repository's
configfile is shared, so remotes, aliases anduser.nameset at repository level apply everywhere. - Hooks. The
hooksdirectory is shared. - Stashes.
refs/stashis shared, so a stash saved in one worktree shows up ingit stash listin all the others.
What is per worktree
Each linked worktree keeps its own copy of the state that describes "where I am right now":
HEAD. Each worktree has its own current branch or detached commit.- The index. Staging is separate, so staging a file in one worktree does not affect another.
- Working files. The checked-out files are ordinary files in that worktree's directory.
- In-progress operation state. Merges, rebases, cherry-picks and bisects are tracked per worktree. A rebase in one worktree does not block work in another.
You can see the split yourself. Run these two commands inside a linked worktree:
git rev-parse --git-dir
git rev-parse --git-common-dirThe first prints the private directory, which looks like /path/to/myproject/.git/worktrees/myproject-hotfix. The second prints the shared repository directory, /path/to/myproject/.git. In the main worktree both commands print the same path.
Seeing sharing in action
Suppose ../myproject-hotfix is a linked worktree. Make a commit there, then look from the main worktree:
cd ../myproject-hotfix
git commit --allow-empty -m "Fix login crash"
cd ../myproject
git log --oneline -1 hotfix/login-crashThe main worktree prints the new commit even though you never fetched or pushed. Both directories read the same object database.
Now the per-worktree side. Stage a change in one worktree, then run git status in the other. The other worktree shows no staged change, because each has its own index.
The consequence for branches
Because refs/heads is shared but HEAD is private, Git can tell which branch each worktree has checked out. That is how it enforces the rule that a branch can be checked out in only one worktree at a time. If you try to check out a branch that another worktree already holds, Git refuses and names the path that has it.
A related effect: if you commit on a branch in one worktree, that branch moves for everyone. The main worktree's git status is not told about it unless that branch is checked out there. When it is, you would see the working files differ from the new tip. The one-branch rule prevents this situation.
Common errors
- Expecting stashes to be local. A stash is shared. Run
git stash listbeforegit stash pop, because the top entry may have come from another worktree. - Expecting config to differ. Setting
git config user.emailwithout--worktreechanges it for all worktrees. Per-worktree config exists, but only whenextensions.worktreeConfigis enabled, and you then write to it withgit config --worktree. - Deleting a branch another worktree uses.
git branch -drefuses to delete a branch that is checked out in any worktree. This is a protection, not a bug. - Confusing the two git directories. In a linked worktree,
.gitis a file, not a directory. It contains agitdir:line pointing to the private directory.
Exercise
You stage a change with git add in worktree A and run git status in worktree B. Will B show the staged change? Then, you run git stash in worktree A. Will git stash list in B show the new entry? Explain both answers.
Show answer
B will not show the staged change, because the index is per worktree. B will show the stash entry, because refs/stash is part of the shared repository state.
Exercise
Which command prints the shared repository directory from inside a linked worktree, and how does its output differ from git rev-parse --git-dir there?
Show answer
git rev-parse --git-common-dir prints the shared repository directory, such as /path/to/myproject/.git. In a linked worktree, git rev-parse --git-dir prints the private directory under .git/worktrees/. In the main worktree both print the same path.