Work About Team Blog Tutorials FAQ
Book a discovery call → Chat on WhatsApp

Home / Blog / DevOps & Cloud / Git Worktrees Explained: Why They're Suddenly Trending (Thanks to AI Coding Agents)

GitGitHubDeveloper ToolsAI Coding AgentsProductivity

Git Worktrees Explained: Why They're Suddenly Trending (Thanks to AI Coding Agents)

If you have followed developer discussions or coding-agent changelogs recently, you have probably seen git worktree mentioned as if it were a new invention. In reality, Git introduced worktrees back in 2015 (version 2.5). The sudden surge in interest has less to do with Git itself and more to do with how modern developer tooling runs.

Here is what a worktree actually does, what it costs you, and why it matters today.


What a Worktree Actually Is

Normally, a Git repository gives you one working directory tied to one branch at a time. Need to look at another branch? You git stash, checkout, do your work, checkout back, and stash pop. It works, but it causes friction, especially if you have a dev server running or uncommitted changes you do not want to touch yet.

A worktree lets you check out multiple branches at once, in separate folders, all sharing the same underlying repository:

# from inside your existing repo, currently on `main`
git worktree add ../myproject-feature-x feature-x

That creates a sibling folder checked out to feature-x. Your original folder stays on main, untouched. Both are functional checkouts of the same repository rather than separate clones.

git worktree list              # see everything active
git worktree remove <path>     # done with it, clean up
ARCHITECTURE: SINGLE DIRECTORY VS. WORKTREES Disk Layout
Standard Clone (1 Branch at a Time)
DIRECTORY: /myproject .git/ main tracked files Stash required to switch

Single folder. Switching branches rewrites all files on disk, stopping running dev processes.

Git Worktree (Parallel Checkouts)
.git/ Shared DB ./myproject branch: main ../feature-x branch: feature-x ../agent-bugfix branch: fix/auth

Multiple directories stay checked out simultaneously, sharing one history database with zero stashing.


Disk Space Costs: What Actually Gets Duplicated

The .git directory (commit history, refs, objects, and trees) is shared across all worktrees. Git stores that data once, regardless of how many worktrees you create.

What does get duplicated is the set of checked-out files for each branch. On a sample repository measured during testing:

Size
.git (history, shared)67 MB
Working tree files (duplicated per worktree)~76 MB

Adding a worktree cost roughly +76 MB instead of +143 MB. You are not re-cloning the repository, only checking out an additional set of source files.


What a Worktree Does Not Give You: Your Environment

A worktree only gives you Git-tracked files. Anything ignored by .gitignore or living outside the repository must be configured separately:

  • node_modules: Always gitignored and never copied across. Each worktree requires its own npm install, yarn install, or pnpm install before running.
  • Virtual environments, .env files, and local services: Python virtualenvs, database connections, and local secrets live outside version control, so each worktree needs its own configuration.
  • Build caches: Local compiler and bundler caches are not shared by default.
RESOURCE SHARING BREAKDOWN What Git Handles vs. What You Handle
📦 Shared in .git/
Stored Once
  • Full commit log and diffs
  • Branch and tag references
  • Remotes and fetch configs
  • Git reflog and object blobs
📄 Duplicated per Tree
Source Only
  • Tracked source files
  • Static assets in repository
  • Branch index and staging
  • Working directory state
⚙️ Isolated per Tree
Manual Setup
  • node_modules/ and virtualenvs
  • .env and local secrets
  • Vite / Next.js server ports
  • Compiler caches (.next, dist)

A couple of practical notes for frontend projects:

  • pnpm softens the disk cost. It uses a global content-addressable store and hardlinks packages into each node_modules. Multiple worktrees add minimal disk overhead even though each still requires an install pass. Standard npm and classic yarn do not do this; each worktree gets a standalone copy of every dependency.
  • Do not symlink node_modules between worktrees unless you are certain both branches have identical dependencies. If package.json or the lockfile diverged between branches, a shared node_modules will cause runtime errors on one of them.

The Other Gotcha: Port Conflicts

If you launch dev servers in two worktrees simultaneously (for instance, running vite in both), both processes will attempt to bind the same default port. The second process either fails outright or grabs another port unpredictably, leading to confusing debugging sessions where you inspect the wrong server.

Common solutions:

  • Override the port manually per worktree: vite --port 3001, next dev -p 3001, or PORT=3001 npm run dev.
  • Use a local environment file: Place a .env.local inside each worktree (gitignored) specifying a distinct PORT.
  • Let the tooling auto-increment: Vite and Next.js can assign the next available port automatically. This works for quick checks, though tracking which branch runs on which port requires attention.
  • Containerize with Docker Compose, assigning isolated dynamic port mappings per instance.

There is no built-in port negotiation in Git or npm. You coordinate ports manually, exactly as you would with two distinct repository clones.


Three developments converged:

  1. Parallel AI coding agents. Tools that run multiple agents concurrently (such as one investigating an issue while another writes tests) require isolated working directories. Worktrees provide isolated checkouts from the same local repository without race conditions or overwriting uncommitted work.
  2. Higher context-switching overhead. Long-running dev processes, TypeScript language servers, and file-watching containers make the traditional stash checkout stash pop cycle slow down local iteration.
  3. A mature feature meeting a new use case. Git worktrees existed quietly for years. Parallel, agent-assisted workflows simply made multi-directory checkouts a practical daily necessity.
PARALLEL AI AGENT ORCHESTRATION Multi-Agent Isolation
👤 DEVELOPER
./myproject
branch: main
Port: :3000
Active UI Design
🤖 AGENT 1 (BUGFIX)
../agent-fix
branch: fix/auth
Port: :3001
Running Unit Tests
🤖 AGENT 2 (FEATURE)
../agent-feat
branch: feat/api
Port: :3002
Schema Migration
▲ All isolated directories link directly to ▲
CENTRAL .git REPOSITORY
Shared commit log, branch heads, and object store across all active sessions

Each worker operates in an isolated filesystem path on dedicated ports without git lock conflicts or overwriting dirty working files.


Summary

Worktrees solve a clear problem: they share Git history, avoid full repository duplicates, and keep multiple branches active in parallel without stashing. At the same time, they require manual environment management: dependencies must be installed per folder, and dev server ports need coordination. For sequential solo work, standard checkouts are often enough. When running automated coding agents alongside your own workflow, worktrees become indispensable.

Building tooling, dashboards, or full-stack products that need a solid engineering workflow behind them? Partner with the developers at Klickspell.

Full-Stack Web Engineering

Need technical precision for your web stack?
Architected for performance & scale.

From modern headless commerce to enterprise infrastructure and clean frontend state systems, Klickspell delivers senior engineering craft without agency bloat.

Book Free Technical Call → Explore Headless Engineering ↗

Keep Reading

Related Engineering Articles

GitHubGit

Get Your GitHub Account Started: A Step-by-Step Beginner Developer Guide

A practical walkthrough for new developers: creating a GitHub account, configuring local Git user profiles, setting up SSH keys, and publishing your first repository.

5 min read Read →
VS CodeAI

Antigravity Was Draining My Laptop Battery, So I Went Back to Plain VS Code

Antigravity was eating my battery and RAM. Here's the lighter setup I use instead: plain VS Code, Continue with Gemini Flash, Claude Code, and the Antigravity extension to utilize a Google AI Pro subscription.

8 min read Read →

Technical Blog

Explore all 50+ engineering tutorials

All Articles ↗

Looking for custom
Shopify engineering craft?

Let's build
your store right.

Book a free 30-minute call. We'll talk through your store goals, timeline, and budget.

Free 30-min call · Discuss scope & budget

Book your free call → Chat on WhatsApp

The craft in every click.