Lesson 0004: Advanced Worktree Patterns

Part of Git Worktrees for Parallel Agentic Coding  |  ~12 minute read + exploration

You've mastered the fundamentals. Now let's explore patterns that turn worktrees from "useful" into "indispensable" โ€” the workflows that make you faster than developers who don't use them. These aren't edge cases; these are patterns you'll reach for regularly.

Pattern 1: The Hotfix Interrupt

You're deep in a feature branch. Production is down. You need to fix main NOW.

# Without worktrees: stash, checkout main, fix, commit, push,
# checkout feature, stash pop, hope nothing went wrong.

# With worktrees:
git worktree add ../my-project-hotfix main
cd ../my-project-hotfix
# Fix the bug, commit, push
git push origin main
cd -
git worktree remove ../my-project-hotfix
# Your feature worktree is untouched. Zero context switch.
Without worktrees, a hotfix forces you to abandon your mental context โ€” stash work-in-progress, switch branches, fix the bug, switch back, unstash. With a worktree, your feature branch stays exactly as you left it. You context-switch by cd-ing to a different directory, not by mutating your workspace.
Hotfix worktrees should be temporary and short-lived. Create them at the moment of crisis, fix the issue, push, and remove the worktree. The main branch stays checked out in the main worktree โ€” the hotfix worktree exists only for the duration of the fix.

Pattern 2: Code Review Worktree

Review a PR without polluting your working directory.

# Fetch the PR and create a review worktree:
git fetch origin pull/42/head:pr-42
git worktree add ~/dev/worktrees/review-42 pr-42

# Review the code in isolation:
cd ~/dev/worktrees/review-42
# Read, test, leave comments...
# When done:
cd -
git worktree remove ~/dev/worktrees/review-42
git branch -D pr-42
Reviewing a PR in your main worktree means you either checkout the PR branch (losing your place) or read diffs on GitHub (limited). A review worktree gives you the full power of your editor, linter, and test suite โ€” without disturbing your current work.

Pattern 3: The Bisect Worktree

Find which commit introduced a bug โ€” while your main worktree stays productive.

# Create a dedicated bisect worktree:
git worktree add ../my-project-bisect main
cd ../my-project-bisect

# Run the bisect:
git bisect start
git bisect bad HEAD
git bisect good v2.5.0

# Test each commit...
git bisect run npm test   # automated
# or test manually:
git bisect good   # or git bisect bad

# When found, record the commit hash and clean up:
git bisect reset
cd -
git worktree remove ../my-project-bisect
git bisect checks out different commits as it narrows the search. In a normal repo, this destroys your working tree state. A bisect worktree isolates the bisection โ€” your main worktree stays on your feature branch, untouched.
git bisect modifies HEAD in the bisect worktree only. The shared refs mean the bisect state (refs/bisect/*) is technically visible everywhere, but the checkout changes only affect the bisect worktree's working tree. Other worktrees are unaffected.

Pattern 4: The Experiment Sandbox

Try a radical refactor. If it fails, delete the worktree. No harm done.

# Create a sandbox worktree for the experiment:
git worktree add ~/dev/worktrees/experiment-refactor -b experiment/refactor-core

cd ~/dev/worktrees/experiment-refactor
# Try wild things. Rewrite the core module. Delete half the codebase.
# If it works:
git push origin experiment/refactor-core
# Open a PR, get feedback.

# If it fails:
cd -
git worktree remove --force ~/dev/worktrees/experiment-refactor
git branch -D experiment/refactor-core
# No trace left behind.
Fear of breaking things slows down exploration. A sandbox worktree eliminates that fear โ€” it's a throwaway environment where failure has zero cost. This is especially powerful for agentic coding: let an agent loose in a sandbox worktree with instructions to "try a completely different approach" without worrying about collateral damage.

Pattern 5: Long-Running Worktree for main

Keep main always checked out and clean, available at a moment's notice.

# Set up once:
git worktree add ~/dev/worktrees/main main

# Your workflow:
# - Main worktree: your current feature branch (changes daily)
# - ~/dev/worktrees/main: always on main, always clean, always up to date

# Before starting any merge:
cd ~/dev/worktrees/main
git pull origin main
cd ~/dev/my-project
git merge main    # main worktree now has latest main merged in
Having main always available in a dedicated worktree eliminates the "what branch am I on?" friction when you need to check something on main. It also serves as a clean reference point โ€” you can always diff your feature branch against the known-good main checkout.

Pattern 6: Multi-Agent Coordination Hub

Orchestrate multiple AI coding agents working on different features simultaneously.

# Your setup (one-time):
# Main worktree (~/dev/my-project): your "control tower" โ€” main branch
# Worktree ~/dev/worktrees/agent-1: feat/dark-mode
# Worktree ~/dev/worktrees/agent-2: fix/login-timeout
# Worktree ~/dev/worktrees/agent-3: refactor/api-layer

# Agent workflow (per agent):
# 1. Agent works in its assigned worktree
# 2. Agent commits and pushes its branch
# 3. You review from the main worktree
# 4. You merge from the main worktree
# 5. You remove the agent's worktree

# Script helper:
agent-worktree() {
  local agent="$1"
  local branch="$2"
  local path="$HOME/dev/worktrees/agent-$agent"

  git worktree add "$path" -b "$branch" && echo "Agent $agent ready at $path"
}

agent-done() {
  local agent="$1"
  git worktree remove "$HOME/dev/worktrees/agent-$agent"
}
This pattern is the culmination of the course. Each agent gets an isolated, stable workspace. The main worktree is your integration point โ€” it stays on main, receives merges, and never has WIP. You can spin up or tear down agent worktrees in seconds.

Pattern 7: CI/CD Debugging Worktree

Debug a CI failure that only happens on a specific commit.

# CI says commit abc1234 is broken. Reproduce locally:
git worktree add --detach ~/dev/worktrees/ci-debug abc1234
cd ~/dev/worktrees/ci-debug
# Reproduce the failure, debug, find the fix.
# Create a branch from here if needed:
git switch -c fix/ci-regression
# Apply the fix, push, create PR.
cd -
git worktree remove ~/dev/worktrees/ci-debug
Debugging a CI failure at a specific commit is a one-off task โ€” you shouldn't need to disturb your main workspace for it. A detached worktree at the failing commit gives you an exact reproduction environment with zero setup cost.

Putting It All Together: Your Worktree Layout

After this course, here's what a well-organized worktree setup looks like for daily agentic coding:

~/dev/ โ”œโ”€โ”€ my-project/ โ† MAIN WORKTREE โ†’ main branch (control tower) โ”‚ โ””โ”€โ”€ .git/ โ† shared git database โ”‚ โ””โ”€โ”€ worktrees/ โ”‚ โ”œโ”€โ”€ main/ โ”‚ โ”œโ”€โ”€ agent-1/ โ”‚ โ”œโ”€โ”€ agent-2/ โ”‚ โ””โ”€โ”€ ... โ””โ”€โ”€ worktrees/ โ”œโ”€โ”€ main/ โ† long-running: main always available โ”œโ”€โ”€ agent-1/ โ† temporary: feat/dark-mode (Agent 1) โ”œโ”€โ”€ agent-2/ โ† temporary: fix/login-timeout (Agent 2) โ”œโ”€โ”€ review-42/ โ† temporary: PR review โ”œโ”€โ”€ hotfix-2026-06-10/ โ† temporary: emergency fix โ”œโ”€โ”€ bisect-perf-regression/ โ† temporary: bisect session โ””โ”€โ”€ experiment-refactor/ โ† temporary: sandbox exploration
The main worktree is your control tower. It stays on main, always clean. You merge, review, and coordinate from here. All other worktrees are temporary โ€” created for a specific task, removed when done. The main worktree is the one long-running exception because having main always at hand is that useful.

Course Summary: What You Now Know

LessonCore takeaway
0000: Mental Model A worktree is an additional working tree attached to the same .git. Shared database, isolated workspace. The .git in a linked worktree is a pointer file.
0001: Creating & Listing git worktree add creates a worktree in under a second. git worktree list is your dashboard. git worktree remove cleans up safely.
0002: Working in Parallel One worktree per agent per branch. The lifecycle is: create โ†’ work concurrently โ†’ merge โ†’ cleanup. Wrap it in shell functions.
0003: Edge Cases Rebase coordination, detached HEAD, orphaned metadata, branch conflicts, merge conflicts. Your diagnostic toolkit: git worktree list, ls .git/worktrees/, cat .git.
0004: Advanced Patterns Hotfix interrupt, code review worktree, bisect worktree, experiment sandbox, long-running main, multi-agent hub, CI debug.

Final Recap

Primary source: Git Worktrees: A Practical Guide โ€” The GitHub Blog covers several of these patterns. The official docs have full flag documentation for every command used.

Going further: Explore git-bisect for automated bisection, and consider integrating worktree creation into your agent orchestration scripts.

Ask questions: "How do I integrate worktrees with my specific agent orchestration setup?" or "What's the best way to share worktree conventions across a team?" โ€” your agent-teacher can help you adapt these patterns to your exact workflow.