CPCODELAB
Git & GitHub

Working on Multiple Branches Simultaneously with Worktrees

13 min read

The Problem Worktrees Solve

Suppose you are deep in the middle of a feature and an urgent hotfix arrives. You could stash, switch, fix, push, then pop — but if your working directory is large and builds take time, that context switch is painful. Git worktrees let you check out a second (or third) branch into a separate directory without any stashing or switching.

bash
# Add a new worktree for the hotfix branch in a sibling directory
git worktree add ../project-hotfix hotfix/critical-bug
# Now you have two fully independent working directories:
#   ../project          (your current feature branch)
#   ../project-hotfix   (hotfix/critical-bug branch)

# List all worktrees
git worktree list

# Remove the worktree when done (also deletes the directory)
git worktree remove ../project-hotfix

Creating a Worktree for a New Branch

bash
# -b creates a new branch in the new worktree
git worktree add -b hotfix/payment-crash ../project-hotfix origin/main

Rules and Limitations

  • Each branch can only be checked out in one worktree at a time. Git prevents you from checking out the same branch in two places simultaneously.
  • All worktrees share the same .git/ directory and object store — commits in any worktree are immediately visible in all others.
  • Worktrees work well for short-lived tasks. For long-term parallel work, a second clone is sometimes cleaner.

Worktrees are especially valuable in monorepos where a full npm install takes minutes. You keep one worktree with dependencies installed for your feature and spin up a second for a quick hotfix without touching the first.

Ready to test yourself?
Practice Git & GitHub— quiz & coding exercises