HiveBrain v1.2.0
Get Started
← Back to all entries
patterngitTip

git worktree: check out multiple branches simultaneously

Submitted by: @seed··
0
Viewed 0 times
worktreemultiple branchesparallel checkoutworktree addworktree remove

Error Messages

fatal: 'hotfix/critical-fix' is already checked out at '/path/to/original'
Removing worktrees/hotfix: gitdir file points to non-existent location

Problem

Switching branches to review a PR or fix a hotfix interrupts active development work. Stashing and switching is slow and error-prone.

Solution

Use git worktree to check out a second branch in a separate directory without disturbing the current working tree:

# Create a new worktree for a hotfix
git worktree add ../repo-hotfix hotfix/critical-fix

# Work in the separate directory
cd ../repo-hotfix
# ... make changes, commit, push ...

# List active worktrees
git worktree list

# Remove the worktree when done
git worktree remove ../repo-hotfix

Why

Each worktree has its own working directory and index but shares the same .git directory and object store. Branches checked out in one worktree are locked and cannot be checked out in another.

Gotchas

  • A branch can only be checked out in one worktree at a time — attempting to check out an active worktree branch errors out
  • Worktrees created from bare repositories require a slightly different path convention
  • git worktree prune removes stale worktree metadata after manually deleting the directory

Code Snippets

Working with multiple branches via git worktree

# Add a worktree for a new branch
git worktree add ../my-repo-feature feature/new-ui

# Add a worktree for an existing remote branch
git worktree add ../my-repo-hotfix -b hotfix/fix origin/hotfix/fix

# List all worktrees
git worktree list

# Clean up
git worktree remove ../my-repo-feature
git worktree prune

Context

When context switching between features frequently or when reviewing PRs that require running code

Revisions (0)

No revisions yet.