Claude Code Mastery
20 partsTL;DR
Anthropic brought git worktrees to Claude Code. Spawn multiple agents working on the same repo simultaneously - no merge conflicts, no context pollution, and your main branch stays clean.
Read next
Anthropic's Claude Code now supports sub agents - specialized AI workers you can deploy for specific development tasks. Instead of cramming every instruction into a single system prompt, you build a ...
6 min readHow to use Claude Code's Task tool, custom sub-agents, and worktrees to run parallel development workflows. Real prompt examples, agent configurations, and workflow patterns from daily use.
11 min readThe definitive collection of Claude Code tips - sub-agents, hooks, worktrees, MCP, custom agents, keyboard shortcuts, and dozens of hidden features most developers never discover.
25 min readGit worktrees have been quietly useful for years. Most developers never touched them. Now that Claude Code ships with native worktree support, that changes - because the use case that makes them indispensable finally exists.
Multiple agents. Same repo. Working in parallel. Zero conflicts.
One repo, multiple working directories checked out simultaneously. Each directory has its own branch, its own working tree, its own set of changes in flight. But they all share the same underlying git data.
For the broader agentic coding map, read Claude Code Agent Teams, Subagents, and MCP: The 2026 Playbook and Why Skills Beat Prompts for Coding Agents in 2026; they connect this article to the surrounding tool and workflow decisions.
No copying the repo. No symlink hacks. No "I'll just stash this and come back." You have branch A and branch B both open and editable at the same time, in separate directories, right now.
The classic use case was context-switching: you're deep in a feature branch and an urgent hotfix lands. Worktrees let you open the hotfix branch in a new directory without touching your in-progress work. Clean handoff.
With autonomous coding agents, the use case is different. You're not switching context - you're eliminating the need to switch at all.

Before Claude Code will create a worktree, you need two things:
That second requirement trips people up. An empty repo won't work. Make your initial commit first:
git init
git add .
git commit -m "initial commit"
Once that's in place, Claude Code handles everything else. Open a second terminal in the same directory, run Claude Code again, and you have two isolated sessions sharing one git repository.
To demonstrate: point one session at your HTML file and say "add a black background." Point the other at the same file and say "add a purple background." Both agents work. Neither steps on the other. You end up with two branches, two directories, two results - and a clean main branch that touched neither.
Inside Claude's .claude folder you'll find the generated worktree directories. Each gets a randomly generated name (something like clever-munching-toast or spicy-napping-otter). Each has its own path, its own git tracking files, its own .claude config. Totally isolated.
Manual two-terminal setup is fine for simple cases. But Claude Code's real leverage is spawning sub-agents programmatically and pointing each one at its own worktree.
Sub-agents are separate Claude Code threads you can spin up from within a session. They run in parallel, report back metrics, and - crucially - can each get their own isolated git context.
A prompt like this kicks them all off at once:
Spawn five different sub-agents. Create five variations of my HTML file -
each should be a creative SaaS landing page. Use git worktree isolation
for all of them.
Claude Code fans out immediately. Five agents, five branches, five directories. While they run you see a live dashboard: each agent's status, progress, what it's working on. The main thread stays clean. No context bleed between variations.
Five distinct SaaS landing pages from a single sentence. Ten to twenty seconds of prompt writing, then let it run.

Get the weekly deep dive
Tutorials on Claude Code, AI agents, and dev tools - delivered free every week.
From the archive
Feb 19, 2026 • 6 min read
Feb 9, 2026 • 8 min read
Jan 19, 2026 • 12 min read
Jan 13, 2026 • 8 min read
The dynamic approach works. But if you're regularly spinning up the same type of agent, you want it configured once and reused.
Claude Code can create sub-agent definition files - similar to skills - that live in your .claude/agents/ folder. You can ask Claude to generate one in plain English:
Create a front-end developer sub-agent. Use the Haiku model.
Enable worktree isolation.
Claude Code will read its own documentation, pull the relevant schema, and write the file. The resulting agent file looks like this:
---
name: frontend-developer
description: >
A specialized front-end developer agent. Invoked automatically when
UI, CSS, or component work is needed.
model: claude-haiku-4-5
tools:
- Read
- Write
- Edit
- Bash
isolation:
worktree: true
---
You are a senior front-end developer specializing in modern UI implementation.
Focus on clean, semantic HTML, maintainable CSS, and accessible component design.
...
The isolation.worktree: true frontmatter is the key part. Every time this agent spins up, it automatically gets its own worktree. The behavior is baked into the definition - you don't have to remember to set it each time.
Sub-agent files can live globally (~/.claude/agents/) for use across all projects, or locally in the project's .claude/agents/ folder for project-specific agents.
You can also scope which tools each agent has access to. If you want an agent that can only read and write files but can't run shell commands, whitelist exactly that. Tight, predictable agent behavior by default.

Anthropic gave you three distinct entry points:
1. CLI flag (manual) - Open Claude Code in a directory, pass the worktree flag. Useful for one-off sessions or when you want explicit control over which branch you're working on.
2. Dynamic sub-agents (in-session) - Ask Claude to spawn agents with worktree isolation from within a session. Best for exploratory work where you're discovering requirements as you go.
3. Agent frontmatter (persistent config) - Define the agent once in .claude/agents/, set isolation.worktree: true. Every invocation of that agent gets isolation automatically. Best for recurring workflows.
Exploring architecture directions. You're considering two ways to restructure a module. Spawn two agents, let both take a full run at it, compare the results. Code is cheap to write. Exploration is expensive when you have to do it sequentially. Do it in parallel instead.
UI variation testing. Different copy, different layouts, different visual treatments. Spin up N agents, have each produce a variation, review the outputs side-by-side. No manual branch management. No "let me undo this and try something else."
Parallel feature development. Independent features on the same codebase. Two agents, two branches, no coordination overhead between them. When both are done, you merge clean branches - not a tangle of conflicting edits.
Safe experimentation on production code. Main branch never gets touched. Every agent works in isolation. If an agent goes sideways, delete the branch. Nothing in main is at risk.
The underlying principle: when work is independent, it should run in parallel. Worktrees make that structurally sound instead of just hoped-for.

This feature is available in three places now: the Claude desktop app (been there for a few weeks), the CLI with direct flags, and the new agent frontmatter config. Anthropic is clearly committing to this as a first-class primitive.
The pattern it enables - one repository, many parallel agents, each isolated, all collaborative - is how agentic development at scale has to work. You can't have ten agents fighting over the same working directory. Worktrees solve that problem at the git level, which is exactly where it belongs.
The agents are cheap to spawn. The branches are cheap to create. The exploration cost drops dramatically. What's expensive is your attention at the end: reviewing what the agents built and deciding what to keep.
That's a much better tradeoff than sequential, single-threaded development.
Git worktrees let you check out multiple branches from the same repository into separate directories simultaneously. Each directory has its own working tree and index, but they share the underlying git objects. This means you can work on branch A in one folder and branch B in another - no stashing, no cloning, no conflicts.
When multiple AI agents work on the same codebase in parallel, they need isolation. Without worktrees, two agents editing the same file would create immediate conflicts. Worktrees give each agent its own clean working directory and branch, so they can work independently without stepping on each other's changes.
Three ways: (1) Use the worktree CLI flag when starting a session, (2) ask Claude to spawn sub-agents "with worktree isolation" dynamically, or (3) set isolation.worktree: true in an agent definition file in .claude/agents/. The third option is best for recurring workflows - the isolation becomes automatic.
Your repository needs at least one commit. An empty repo with no commits won't work. Run git init && git commit --allow-empty -m "init" if you're starting fresh. After that, Claude Code handles worktree creation automatically.
Sub-agents can communicate via the SendMessage tool or by writing to shared files. However, each worktree has its own working directory, so file changes in one worktree don't appear in another until merged. For structured handoffs, have agents write results to a predictable location or use explicit message passing.
Claude Code creates worktrees in the .claude/ folder with auto-generated names. You can delete them manually, or use git worktree remove <path> to clean them up. The branches they created remain in your repo until you delete those too. Main branch stays untouched throughout.
There's no hard limit in git or Claude Code. The practical limit is your machine's resources (disk space, memory) and your Anthropic usage quota. Five to ten parallel agents is common for exploration tasks. For heavy parallelization, consider using the Haiku model for sub-agents to reduce token costs.
Official docs:
This article is based on a Developers Digest video. All feature behavior is based on direct testing with Claude Code at time of publication.
Further Reading:
Technical content at the intersection of AI and development. Building with AI agents, Claude Code, and modern dev tools - then showing you exactly how it works.
Anthropic's agentic coding CLI. Runs in your terminal, edits files autonomously, spawns sub-agents, and maintains memory...
View ToolOpenAI's coding agent for terminal, cloud, IDE, GitHub, Slack, and Linear workflows. Reads repos, edits files, runs comm...
View ToolAI coding platform built for large, complex codebases. Context Engine indexes 500K+ files across repos with 100ms retrie...
View ToolOpen-source terminal coding agent from Moonshot AI. Powered by Kimi K2.5 (1T params, 32B active). 256K context window. A...
View ToolUnlock pro skills and share private collections with your team.
View AppPro hooks for Claude Code. Private bundles, team sync, one-click install.
View AppCatch broken SKILL.md files in CI before they hit your team.
View AppIsolated git worktrees for parallel Claude Code sessions.
Claude CodeConfigure Claude Code for maximum productivity -- CLAUDE.md, sub-agents, MCP servers, and autonomous workflows.
AI AgentsA complete, citation-backed Claude Code course with setup, prompting systems, MCP, CI, security, cost controls, and capstone workflows.
ai-development
Nimbalyst Demo: A Visual Workspace for Codex + Claude Code with Kanban, Plans, and AI Commits Try it: https://nimbalyst.com/ Star Repo Here: https://github.com/Nimbalyst/nimbalyst This video demos N...

Composio: Connect AI Agents to 1,000+ Apps via CLI (Gmail, Google Docs/Sheets, Hacker News Workflows) Check out Composio here: http://dashboard.composio.dev/?utm_source=Youtube&utm_channel=0426&utm_...

Anthropic has released Channels for Claude Code, enabling external events (CI alerts, production errors, PR comments, Discord/Telegram messages, webhooks, cron jobs, logs, and monitoring signals) to b...

Anthropic's Claude Code now supports sub agents - specialized AI workers you can deploy for specific development tasks....

How to use Claude Code's Task tool, custom sub-agents, and worktrees to run parallel development workflows. Real prompt...

The definitive collection of Claude Code tips - sub-agents, hooks, worktrees, MCP, custom agents, keyboard shortcuts, an...

Claude Code is Anthropic's terminal-based AI agent that ships code autonomously. Complete guide: install, CLAUDE.md memo...

Anthropic dropped a batch of updates across Claude Code and Cowork - remote control from your phone, scheduled tasks,...

Claude Code now has a native Loop feature for scheduling recurring prompts - from one-minute intervals to three-day wi...

New tutorials, open-source projects, and deep dives on coding agents - delivered weekly.