Elias Lecomte

Git Worktrees in the Age of AI Agents

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

Tags: Git, AI, Developer Experience.

An urgent production bug is a familiar reason to use git worktree: you need to work on a fix while leaving an unfinished feature and its uncommitted changes alone.

AI coding agents give me another reason to use it. A refactor, a set of regression tests, or a bug investigation can keep an agent busy for several minutes. I want to keep working during that time. Switching branches or editing files in the same workspace can interfere with the agent, causing file locks, race conditions, or test failures. A separate worktree gives us each a working directory.

Why not just clone again?

You can clone the repository into another folder, such as my-repo-2. That gives you a separate workspace, but also duplicates the .git history. Commits made in one clone aren't immediately visible in the other without pushing and fetching, and you have another set of remotes, hooks, and local credentials to configure.

A worktree attaches another working directory to the same .git database. The directories share local commit history and refs, while each has its own branch checked out.

Parallel development with AI harnesses

The workflow is the same whether you use Antigravity, Cursor, Claude Code, or a custom agent harness:

  1. Create a separate 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.

If your agent tool has an option to create a worktree when starting a session, it can handle that first step for you.

Worktree commands

Create a worktree for an existing branch

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

Create a worktree on a 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

  • Git refuses to check out the same branch in more than one worktree at a time.
  • 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.