Git Worktrees: Parallel Branching Environments
In standard Git workflows, switching branches requires updating your single working directory. If you are midway through compiling a massive project with long build times or uncommitted experiments and need to switch branches to review a PR or fix an emergency hotfix, stashing or stowing your workspace introduces friction.
Git Worktrees (git worktree) allow you to check out multiple branches simultaneously into separate directory folders while sharing a single underlying .git repository and object database!
┌─────────────────────────────────────────────────────────────┐
│ Git Worktree Shared Object Architecture │
│ │
│ Central .git Database │
│ ┌───────────────────────────┐ │
│ │ .git/objects, refs, logs │ │
│ └───────────────────────────┘ │
│ ▲ ▲ │
│ ┌────────────┘ └────────────┐ │
│ ▼ ▼ │
│ Directory 1 (/projects/main) Directory 2 (/projects/hotfix)
│ Checked out to 'main' Checked out to 'hotfix-102'
│ (Running production dev server) (Editing emergency fix)
└─────────────────────────────────────────────────────────────┘
1. Creating and Managing Worktrees
# 1. Add a new worktree folder checked out to a new branch
git worktree add ../front-heaven-hotfix -b hotfix/login-crash
# 2. List all active worktrees and their checked-out branches
git worktree list
Output:
/Users/hesam/projects/front-heaven 7f8a9b0 [main]
/Users/hesam/projects/front-heaven-hotfix 3c4d5e1 [hotfix/login-crash]
You can now open ../front-heaven-hotfix in a completely separate VS Code window, run tests, commit, and push without ever interrupting your development server in front-heaven!
2. Removing Worktrees
# Remove worktree folder and release branch lock
git worktree remove ../front-heaven-hotfix
# Clean up stale worktree administrative metadata
git worktree prune
Summary & Key Takeaways
git worktreeenables multiple working directories linked to a single.gitrepository.- Eliminates context-switching friction when executing urgent hotfixes or long build workflows.
- All worktrees share the same commit history, stashes, and Git objects without duplicating disk space.
Best Practices & Senior Guidance
- Use Worktrees for Long-Running Reviews: Check out heavy teammate PR branches into a dedicated
../review-worktreefolder to avoid invalidating local build caches. - Never Check Out the Same Branch Twice: Git strictly forbids checking out the same branch in two different worktrees simultaneously to prevent race condition corruptions.