Git Worktree: Work on Multiple Branches Simultaneously

Learn how to use git worktree to check out multiple branches at once in separate directories. Perfect for hotfixes, code reviews, and parallel development.

Advanced

Detailed Explanation

Working on Multiple Branches with Git Worktree

git worktree lets you check out multiple branches of the same repository in separate directories simultaneously. No more stashing, switching, or cloning.

Creating a Worktree

# Create a worktree for an existing branch
git worktree add ../hotfix hotfix/critical-fix

# Create a worktree with a new branch
git worktree add -b feature/new-feature ../new-feature

# Create a worktree from a specific commit/tag
git worktree add ../v2-fix v2.0.0

Listing Worktrees

git worktree list

Output:

/home/user/project        abc1234 [main]
/home/user/hotfix         def5678 [hotfix/critical-fix]
/home/user/new-feature    ghi9012 [feature/new-feature]

Removing a Worktree

# Remove a worktree (after committing/pushing changes)
git worktree remove ../hotfix

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

# Prune stale worktree references
git worktree prune

Common Workflows

Hotfix while developing:

# You are working on a feature in /project
# A critical bug comes in:
git worktree add ../hotfix main
cd ../hotfix
git checkout -b hotfix/urgent
# Fix the bug, commit, push
cd ../project
# Continue your feature work

Code review:

# Review a PR without leaving your branch
git worktree add ../review origin/pr-123
cd ../review
# Run tests, review code
cd ../project
git worktree remove ../review

Worktree vs Stash vs Clone

Approach Speed Disk Space Shares .git
Stash Fast None Yes
Worktree Fast Small Yes
Clone Slow Large No

Worktrees share the same .git directory, so they use minimal extra disk space and all operations (fetch, gc) apply to all worktrees.

Limitations

  • A branch can only be checked out in one worktree at a time
  • Worktrees share the same stash and reflog
  • Some GUI tools may not fully support worktrees

Use Case

Git worktrees solve the common problem of needing to work on multiple things at once. Instead of stashing and switching branches (which disrupts your flow), you can have separate directories for each branch. This is ideal for: handling urgent hotfixes without losing context on your current feature; reviewing pull requests while coding; running long tests on one branch while developing on another; and comparing behavior between branches side by side.

Try It — Git Command Reference & Cheat Sheet

Open full tool