The hidden gem: Work on multiple branches simultaneously without stashing or cloning.
You're deep in a feature branch, files changed everywhere, and someone asks you to review a hotfix on main. Your options?
git stashβ switch β review β switch back βgit stash pop(messy, fragile)- Clone the repo again (wasteful, slow)
git worktreeβ instant parallel checkout, zero disruption β¨
A worktree is an additional working directory linked to the same .git database. Each worktree checks out a different branch. They share history, remotes, and config β but each has its own working files and index.
repo/ β your main worktree (feature-branch)
repo-hotfix/ β linked worktree (main branch)
repo-experiment/ β linked worktree (experiment branch)
One .git, multiple working directories. Mind = blown.
# Add a worktree for an existing branch
git worktree add ../hotfix-tree main
# Add a worktree AND create a new branch
git worktree add -b bugfix/issue-42 ../bugfix-tree main
# List all worktrees
git worktree list
# Remove a worktree when done
git worktree remove ../hotfix-tree
# Prune stale worktree references
git worktree prune- Each branch can only be checked out in ONE worktree at a time. Git enforces this.
- Worktrees share the object database β no wasted disk space.
- Each worktree has its own
HEAD, index, and working directory. - Worktrees are local only β they don't affect remotes or collaborators.
You're building a feature on feature/search but need to fix a bug on main:
git worktree add ../hotfix main
cd ../hotfix
# fix the bug, commit, push
cd ../gitfoo_episode_1
git worktree remove ../hotfixYour feature branch is completely untouched.
git worktree add ../test-main main
git worktree add ../test-feature feature/new-ui
# Run test suites in parallel in separate terminalsgit worktree add ../review origin/pull-request-123
cd ../review
# read the code, run it, test it- Name worktree dirs clearly β include the branch name or purpose.
- Use
git worktree listto see what you have checked out and where. - Combine with
git bisect(Episode 2) β bisect in a worktree while your main tree stays clean. - Worktrees are perfect for CI/CD scripts that need to build multiple branches.
bash episodes/demos/01-worktrees-demo.sh| Command | What it does |
|---|---|
git worktree add <path> <branch> |
Create a new worktree |
git worktree add -b <new> <path> <start> |
Create worktree + new branch |
git worktree list |
Show all worktrees |
git worktree remove <path> |
Remove a worktree |
git worktree prune |
Clean up stale references |
git worktree lock <path> |
Prevent a worktree from being pruned |
git worktree move <path> <new-path> |
Relocate a worktree |