Sub topic 1
The problem: one repository, one checked-out branch
A Git repository stores history for many branches, but a normal clone has one working directory, so only one branch is checked out at a time. This chapter explains why that limit hurts and introduces the feature that removes it, the linked worktree.
Prerequisites
This chapter assumes you can:
- Commit, create branches, switch between them, and merge.
- Tell the working directory, the index (staging area), and the repository history apart.
- Use a command line and relative paths such as
../project-fix. - Run
git fetchand name remote-tracking branches such asorigin/main.
You need Git 2.17 or newer. Earlier versions lack git worktree remove, which a later chapter uses.
The cost of switching branches
Suppose you are halfway through a feature on feature/search when a production bug arrives. Your working directory holds uncommitted edits, and the fix belongs on a branch cut from main. With one working directory, you have three usual options:
- Stash. Run
git stash, switch, fix, switch back, thengit stash pop. This works until a stash fails to apply cleanly, or you forget which stash holds what. - Make a throwaway commit. A "WIP" commit saves your edits, but it pollutes history unless you rewrite it later.
- Clone again. A second clone gives you a second directory, but it duplicates the whole object database and does not share branches or stashes with the first clone.
Switching also has side effects. Git rewrites every file that differs between the two branches. Their timestamps change, so build tools see changes and rebuild. A running test run, dev server, or long build must stop first, because the files under it are changing.
What a linked worktree is
A linked worktree is an additional working directory attached to an existing repository. It has its own checked-out branch (or commit), its own index, and its own HEAD. It shares the object database, branches, tags, and remotes with the repository it was created from.
The directory you originally cloned into is the main worktree. Every directory you add afterward is a linked worktree. Both kinds work the same way for everyday commands such as git status, git commit, and git log.
A first look
This command creates a new directory beside your repository, creates the branch hotfix/login-crash from main, and checks it out there:
git worktree add ../myproject-hotfix -b hotfix/login-crash mainYour original directory is untouched. Its uncommitted edits stay where they are, and nothing needs stashing. You can open the new directory in a second terminal or editor window, fix the bug, and commit.
A linked worktree has a .git entry that is a small file, not a directory. It points back to the repository:
cat ../myproject-hotfix/.gitThe output looks like gitdir: /home/you/myproject/.git/worktrees/myproject-hotfix. That path holds the worktree's private state, such as its HEAD and index. The next chapters cover this in detail.
Worktree versus second clone
| Aspect | Second clone | Linked worktree |
|---|---|---|
| Object database | Full copy | Shared |
| Disk use | Whole history again | Only the checked-out files |
| Branches and tags | Separate sets | One shared set |
| Commit visible in the other directory | Only after push and fetch | Immediately |
| Same branch checked out twice | Allowed | Refused by default |
The last row is the main trade-off. Because branches are shared, Git prevents two worktrees from moving the same branch at once. A later chapter covers that rule and how to resolve it.
Common mistakes
- Creating the worktree inside the repository directory. Place it as a sibling, such as
../myproject-hotfix, so the main worktree does not see it as untracked files. - Deleting a worktree directory with
rm -rf. Git keeps metadata about it, which causes stale entries. Chapter on cleanup shows the proper commands. - Expecting files such as
node_modulesor.envto appear in the new directory. Untracked and ignored files are not shared.
Exercise
You are midway through uncommitted edits on feature/search and must start a fix from main. Name one drawback of using git stash and one advantage of a linked worktree here.
Show answer
A stash can conflict when popped or be forgotten, and switching rewrites files, which triggers rebuilds. A linked worktree leaves your edits in place in the original directory and gives the fix its own directory, while still sharing history with no second clone.