Elias Lecomte

Git Worktrees in the Age of AI Agents

Written on Monday, 28 September 2026 at 20:15.

Tags: Git, AI, Developer Experience.

For years, git worktree was one of those features that developers knew existed, but rarely used. The textbook use case was always: you’re in the middle of a complex feature, an urgent production bug lands on your desk, and you don’t want to stash uncommitted changes or risk dirtying your branch.

Enter AI coding agents.

Today, when you delegate an end-to-end task to an agent—refactoring a module, writing regression tests, or investigating a bug—it might take several minutes to finish. While the agent is working, what do you do?

If you try to switch branches or edit code in the same workspace, you’ll trigger file locks, race conditions, and test failures under the agent's feet.

This is where Git worktrees shine.

Why not just clone again?

Before worktrees, the default workaround was cloning the repository into another folder: my-repo-2.

That works, but it has major downsides:

  • Wasted disk space: You duplicate the entire .git history.
  • Out-of-sync references: Commits made in one clone aren't immediately visible in the other without pushing and fetching.
  • Setup friction: You need to reconfigure remotes, hooks, and local credentials.

A worktree allows you to attach multiple independent working directories to a single .git database. They share the same local commit history and refs, but each directory is checked out to its own branch.

Parallel development with AI harnesses

Whether you use Antigravity, Cursor, Claude Code, or custom agent harnesses, running agents in dedicated worktrees is becoming standard.

Instead of blocking your editor while an agent runs:

  1. Spin up an isolated worktree for the agent.
  2. Let the agent execute in the background.
  3. Keep coding your own tasks uninterrupted in your main working tree.
  4. Review the agent's branch and merge when ready.

Most modern agentic tools now have a toggle to do this automatically when starting a new session.

Quick Cheatsheet

Spinning up and managing worktrees takes just a few commands:

Create a worktree for an existing branch

git worktree add ../project-hotfix hotfix/login-bug

Create a worktree on a brand new branch

git worktree add -b feat/agent-refactor ../project-refactor main

List all active worktrees

git worktree list

Safely remove a worktree

When the task is done and the branch is merged:

git worktree remove ../project-refactor

Clean up orphaned metadata

If you deleted a worktree folder manually via your file explorer:

git worktree prune

Gotchas to keep in mind

  • One branch per worktree: Git will refuse to check out the same branch in more than one worktree simultaneously.
  • Untracked files & secrets: Local files like .env, untracked config, and node_modules are not shared automatically. You may need to copy your .env or run your package manager install in the new directory.

If you haven't integrated worktrees into your day-to-day development yet, give them a spin—especially the next time you let an AI agent tackle a heavy task in the background.