Multiple Working Trees with git worktree

Need to review a PR while your own work sits half-finished? git worktree lets you check out multiple branches into separate directories that all share one .git — no stashing, no second clone. This lesson covers adding, listing, and removing worktrees, when to use them, and the one rule that keeps them safe.

Learn Multiple Working Trees with git worktree in our free Git course — a beginner-friendly interactive lesson with worked examples, a practice exercise and…

Part of the free Git course at LearnCodingFast — hands-on lessons with examples you run in your browser, plus practice exercises and a quick quiz.

1️⃣ Adding a Worktree

git worktree add ../path branch checks out branch into a brand-new directory at ../path . Your current directory stays exactly as it is — perfect for reviewing a PR or running a build without disturbing your in-progress work. Both folders share the same underlying .git , so there's no extra clone.

2️⃣ Listing Worktrees

git worktree list shows every working tree attached to the repository: its path, the commit it's on, and the branch checked out there. The first line is your main worktree (the original clone); any you've added are linked worktrees .

3️⃣ Removing & Pruning

Finished with a worktree? git worktree remove ../path deletes the folder and its bookkeeping in one clean step. If you deleted the folder by hand instead, Git keeps stale records — clear them with git worktree prune .

4️⃣ The One Rule: No Branch Twice

A branch can be checked out in only one worktree at a time. Two worktrees on the same branch would fight over its tip and corrupt your state, so Git blocks it. If you just need to inspect a branch that's already out, add the worktree with --detach for a detached-HEAD checkout of the same commit.

Your turn. Add a worktree for a branch, list to confirm it, then remove it. Fill in the three blanks.

📋 Quick Reference

No commands given this time — just the plan. Build two branches in parallel from one shared repository.

Practice quiz

What does git worktree add ../review hotfix do?

  • Checks out the hotfix branch into a new directory ../review that shares the same .git
  • Deletes the hotfix branch
  • Creates a brand-new clone of the repository
  • Merges hotfix into the current branch

Answer: Checks out the hotfix branch into a new directory ../review that shares the same .git. git worktree add creates a linked working tree at the given path with the named branch checked out, sharing the original repository's object store.

How many .git object databases do multiple worktrees of one repo share?

  • One per worktree
  • Two
  • One — they all share the main repository's objects
  • None

Answer: One — they all share the main repository's objects. All worktrees share a single .git object store; only the working directory and HEAD/index differ per worktree.

Which command lists every working tree attached to the repository?

  • git worktree show
  • git worktree list
  • git branch --worktrees
  • git list-worktrees

Answer: git worktree list. git worktree list prints each worktree's path, the commit it points at, and the branch checked out there.

Can you check out the same branch in two worktrees at once?

  • Only after a force push
  • Yes, with no restriction
  • Yes, but only on the main branch
  • No — Git refuses to check out a branch that is already checked out in another worktree

Answer: No — Git refuses to check out a branch that is already checked out in another worktree. Git blocks checking out the same branch in two worktrees to prevent two HEADs from updating the same branch and corrupting your state.

After you delete a worktree's directory manually, which command cleans up the stale administrative records?

  • git worktree prune
  • git gc --worktrees
  • git clean -worktree
  • git worktree forget

Answer: git worktree prune. git worktree prune removes bookkeeping for worktrees whose directories no longer exist.

What is the recommended way to delete a worktree you no longer need?

  • rm -rf the folder and nothing else
  • git worktree remove ../path
  • git branch -d
  • git worktree delete

Answer: git worktree remove ../path. git worktree remove deletes the working directory and its administrative files cleanly in one step.

Why use a worktree instead of git stash to review a teammate's PR?

  • Stash cannot hold more than one change
  • Worktrees compress files automatically
  • Stash is slower than a worktree
  • A worktree lets you keep your in-progress work untouched in its own directory while you check out and build the PR branch elsewhere

Answer: A worktree lets you keep your in-progress work untouched in its own directory while you check out and build the PR branch elsewhere. A worktree gives the PR branch its own directory, so your current work stays exactly as it is with no stashing, restoring, or context loss.

Compared with making a second full clone, a worktree mainly saves...

  • Branch names
  • Nothing — they are identical
  • Network bandwidth and disk space, because objects and history are shared rather than copied
  • The need to ever commit

Answer: Network bandwidth and disk space, because objects and history are shared rather than copied. A worktree reuses the existing object database, so you avoid re-downloading and re-storing the entire history that a fresh clone would duplicate.

The original repository directory you cloned into is called the...

  • bare worktree
  • main worktree
  • linked worktree
  • detached worktree

Answer: main worktree. The first/original checkout is the main worktree; any added with git worktree add are linked worktrees.

A good real-world use of worktrees is...

  • Building or testing two branches in parallel directories at the same time
  • Permanently storing backups
  • Replacing git commit
  • Avoiding the need for branches

Answer: Building or testing two branches in parallel directories at the same time. Separate directories let you run parallel builds, tests, or reviews of different branches simultaneously without switching back and forth.