Skip to main content

git worktree โ€” Multiple Working Trees

What It Doesโ€‹

git worktree lets you check out multiple branches simultaneously, each in its own directory, all sharing the same .git repository. No stashing, no switching, no losing context.

This is ideal when you need to review or hotfix another branch without abandoning in-progress work on your current branch.


When to Use Worktreesโ€‹

ScenarioWithout WorktreeWith Worktree
Fix a hotfix while mid-featureStash โ†’ switch โ†’ fix โ†’ push โ†’ pop โ†’ resumeAdd worktree โ†’ fix in parallel โ†’ remove worktree
Review a colleague's large PRStash โ†’ checkout โ†’ review โ†’ switch backAdd worktree for their branch โ†’ review in separate window
Run tests on main while developing on featureWait for tests then switchWorktree on main runs tests independently
Compare behaviour between branchesManually switch back and forthTwo IDE windows, each on a different branch

Basic Usageโ€‹

Add a worktreeโ€‹

# Add a worktree for an existing branch
git worktree add ../hotfix-workspace hotfix/JIRA-999

# Add a worktree and create a new branch
git worktree add -b hotfix/JIRA-999 ../hotfix-workspace origin/main

This creates a new directory ../hotfix-workspace that is a fully functional working tree on the hotfix/JIRA-999 branch, while your current directory stays on your feature branch.

List all worktreesโ€‹

git worktree list
# /home/jane/projects/transaction-service a3f9bc2 [feature/JIRA-123]
# /home/jane/projects/hotfix-workspace 91bc3f0 [hotfix/JIRA-999]

Work in the new worktreeโ€‹

cd ../hotfix-workspace

# It's a full working tree โ€” all normal Git commands work
git log --oneline -5
./mvnw test
git add .
git commit -m "fix(transactions): resolve NPE on null description"
git push origin hotfix/JIRA-999

# Return to your feature work
cd ../transaction-service
# Your feature branch is completely undisturbed โ€” no stash needed

Remove a worktreeโ€‹

# First, leave the worktree directory
cd ../transaction-service

# Remove the worktree
git worktree remove ../hotfix-workspace

# Force remove if there are uncommitted changes
git worktree remove --force ../hotfix-workspace

# Prune stale worktree references (if directory was deleted manually)
git worktree prune

Worktree with IDEโ€‹

Open the worktree directory as a separate project window in your IDE:

# IntelliJ IDEA
idea ../hotfix-workspace

# VS Code
code ../hotfix-workspace

You now have two IDE windows, each on a different branch, each with their own run configurations, search indexes, and tool windows.


Constraintsโ€‹

  • A branch can only be checked out in one worktree at a time. If hotfix/JIRA-999 is in a worktree, you cannot git switch hotfix/JIRA-999 in the main tree until the worktree is removed
  • All worktrees share the same object store โ€” no duplication of history or objects
  • You can have as many worktrees as you need

Useful Commandsโ€‹

CommandMeaning
git worktree add <path> <branch>Create a new worktree for an existing branch
git worktree add -b <branch> <path> <base>Create worktree on a new branch
git worktree listShow all worktrees and their branches
git worktree remove <path>Remove a worktree
git worktree pruneClean up stale worktree references
git worktree lock <path>Lock a worktree to prevent removal
git worktree unlock <path>Unlock a worktree

Worktrees Beat Stashing for Hotfixes

The classic hotfix workflow requires you to stash, switch, fix, push, switch back, and pop โ€” with the risk of forgetting to pop or creating stash conflicts. Worktrees eliminate all of that: your feature work is untouched in its directory, and the hotfix lives in a separate, parallel workspace.

Interview Questions (Senior Level)โ€‹

  1. How do worktrees reduce context-switch risk in incident response workflows?
  2. What operational guardrails should teams define for temporary worktree lifecycle?
  3. When is worktree superior to branch switching and stash-based flows?
  4. How do you avoid branch-lock confusion when one branch is active in another worktree?

Short answer guide:

  • Worktrees isolate changes and minimize accidental carryover.
  • Enforce naming, cleanup, and ownership conventions for temp trees.
  • Prefer for parallel hotfix/review/test contexts.
  • Use git worktree list visibility and cleanup routines.
Interview Focus

Explain how worktrees reduce context-switch risk during hotfixes and parallel reviews.

Interview Trap

Leaving stale worktrees that create branch confusion and cleanup debt.