Pular para o conteúdo
← Back to Skalablog

Published article

Git Worktree: 5 Steps to Parallel AI Coding

Software EngineeringClaude CodeGitHub CopilotAnthropic

Git worktree lets you check out multiple branches at once, each in its own folder, so you can run AI coding agents in parallel without stashing or losing work. This guide covers the core commands, environment setup, bare-repository organization, and how Claude Code and GitHub Copilot use worktrees today.

What git worktree is and why it matters now

Git worktree is a Git feature, available since 2015 in the official Git documentation, that lets one repository hold several working directories, each checked out on a different branch. Instead of stashing changes to switch tasks, you OpenAI second folder and keep both contexts intact.

The feature stayed niche for a decade. Terminal coding agents changed that. Tools like Claude Code, Anthropic terminal-based agentic coding tool, and GitHub Copilot, GitHub's AI pair programmer, can run long tasks in the background. Giving each agent its own folder and branch is the simplest way to run them in parallel without merge chaos.

Why git worktree beats git stash for context switching

Git stash saves tracked modifications out of the way so you can change branches, then restores them later. It works for a quick interruption, but it has structural limits that grow painful with real projects.

A stash only captures what Git tracks. Ignored and untracked files such as .env stay behind in the old folder, so your hotfix branch may point at the wrong database or port. Stashes also stack up without meaningful names, and developers routinely forget what is inside older entries.

A worktree avoids all of that. Each task lives in a full directory on its own branch, with its own untracked files and dependencies. Your feature keeps its dirty state untouched while you fix the bug elsewhere. Stash remains useful for truly temporary saves; worktrees are the right tool when a task takes a different direction.

Core git worktree commands you need

Three commands cover daily use. From any branch, run git worktree add <path> <branch>, or pass -b <new-branch> to create the branch at the same time. List everything with git worktree list. Remove a finished folder with git worktree remove <path>.

Removing a worktree never deletes committed work. Commits live in the repository, so the branch survives after the folder is gone. If the worktree still contains uncommitted files, Git asks for confirmation before removing it, which protects untracked work.

VS Code surfaces worktrees in the Source Control view, where you can open one in a new window or the same session. Other editors with Git integration, including JetBrains IDEs, handle multiple working copies the same way: each folder is a normal project rooted at its own branch.

How to provision the environment in each worktree

A worktree is a copy of what Git tracks. It has no node_modules, no .env, and no local database. Before the folder is usable you need a small setup step, which is good hygiene anyway: a project should be easy to bootstrap from scratch.

Port conflicts are the most common trap. If the main checkout serves on port 3000, the second worktree cannot bind the same port. A short init script can pick a free port and write it into a per-folder .env file. With Docker, bind the fixed port inside the container only and let the host port be assigned dynamically, then record that port in the environment file.

The same applies to databases: create one per worktree, or point each folder at a distinct schema. Once an init script handles dependencies, env, and services, any worktree becomes a runnable copy within minutes.

How AI coding agents use worktrees

Once the repo has independent copies, you can assign one task per agent and run them concurrently. The practical rule from practitioners: pick tasks that touch different files, because two agents editing the same code still produce the same merge conflicts Git always would.

The tools differ in how they create folders. GitHub Copilot's background agents create a worktree named after the project plus a timestamp. Claude Code supports explicit worktree selection and lets you pass a custom path. Both approaches work, but agent-chosen names tend to be verbose and can land inside the project directory, where your editor may start indexing them.

That is why folder organization matters more in 2026 than it did in 2015: you will revisit these folders to review agent output, so each one needs a name and location you can parse months later.

Organizing worktrees with a bare repository

A bare repository holds only Git's object database, with no working files. Hosting services such as GitHub and GitLab use bare repositories server-side, and git clone --bare creates one locally as described in the Git clone documentation.

The bare-clone pattern inverts the usual layout. You clone with --bare into a hidden folder such as .bare, add a .git file pointing at it, and then create every worktree from there. No single folder is privileged. Any worktree can be removed and rebuilt from the bare store at any time, and nothing clutters the project root.

Folder naming is a taste decision: short names like auth next to a feature-auth branch keep terminal navigation fast. zoxide, a smarter cd command that ranks folders by how often you visit them, and fzf, a fuzzy finder, make jumping between many worktrees practical; typing a few path fragments is enough to land in the right one.

One editor-side annoyance: several worktrees all show the branch name, so five tabs can all read main. A .code-workspace file with an explicit folder name, or the window.title setting, fixes the labeling in VS Code and its forks.

Frequently asked questions

  • Does removing a git worktree delete my commits? No. Commits are stored in the repository itself, so the branch and its history survive. Git only warns before deleting a worktree that still holds uncommitted files.
  • Can two worktrees have the same branch checked out? No. Git refuses to check out a branch that another worktree already uses, which prevents two folders from racing on the same branch.
  • Do AI agents need git worktree to run in parallel? Technically they could share one folder, but concurrent edits to the same working copy cause constant conflicts. Separate worktrees give each agent an isolated folder and branch, which is the setup tools like Claude Code and GitHub Copilot now automate.
  • Does git worktree duplicate node_modules and .env? No. The worktree contains only what Git tracks. Dependencies, environment files, and local databases must be provisioned per folder, usually by a small init script.
  • Is a bare repository required for worktrees? No. Worktrees work from a normal clone. The bare-clone layout is an organizational pattern that keeps the object store separate from every working folder, which scales better when you juggle many worktrees.

Turn your own screen sessions into written guides

This walkthrough came from a video, and that is common: developers explain worktrees, agent setups, and environment scripts far more naturally on screen than in prose. Related material on this stack lives at crazystack.com.br, including the Crazystack Typescript track and the Bootcamp do Dev Doido hosted by Gustavo Dev Doido, and every one of those sessions holds knowledge worth reading as an article.

If you record explanations like this, Skala Blog turns a YouTube video into a structured written piece: paste the URL, get the transcription, and generate an article your team and search engines can actually read.

Source video