Git Worktree.
A Git worktree is an additional working folder linked to the same Git repository but with a different branch checked out. It lets you work on several states of a project at the same time, such as a bug fix and a new feature, without constantly switching back and forth. It is particularly useful when several AI agents work in parallel.
Git Worktree — Explained in Detail
Normally a Git repository has exactly one working folder. If you are working on one branch and urgently need to fix a bug on another, you have to stash your changes, switch branch, switch back later and hope nothing gets lost. Worktrees, a fixed part of Git since 2015, solve this: a command such as git worktree add ../shop-fix -b fix/cart creates a second folder with the new branch checked out. Both folders share the same history, but each has its own files.
The advantage over a second copy of the repository is that a worktree is lighter and stays connected. Commits made in one folder are immediately visible in the other; there is only one shared repository and no syncing between copies. Git also prevents the same branch from being checked out in two worktrees at once, which rules out many mix-ups. git worktree list shows all working folders, and git worktree remove cleans one up.
Since coding agents arrived, worktrees have seen a renaissance. Let two agents work in the same folder and they overwrite each other's files, and tests run on a mix of both changes. Give each agent its own worktree and it works in isolation on its branch. At the end the results are merged as usual. Tools are appearing on GitHub that automate creating, starting and cleaning up worktrees for agents.
It is not entirely without drawbacks. Depending on the project, each worktree needs its own installed dependencies, its own environment variables and sometimes its own port for the development server. Shared resources such as a local database are shared by all worktrees, which can cause conflicts. And parallel work only shifts the problem: if two agents change the same file in different places, merging produces a conflict. Clearly dividing tasks by file therefore remains important, see multi-agent system.
The key commands
git worktree add ../project-feature -b feature/new-form creates a new folder with a new branch. git worktree add ../project-hotfix main checks out an existing branch in a further folder. git worktree list shows all working folders and their branches. git worktree remove ../project-feature removes a folder once the work has been merged, and git worktree prune cleans up entries whose folders were deleted manually.
In daily use a naming convention pays off, such as one folder per task next to the main folder. How we organise development with several agents is described on AI-assisted development.
Example: two agents on a club website
A constructed example: a sports club in St. Gallen is having its website developed further. Two tasks are due at the same time: a registration page for the summer camp and a fix for a display bug in the fixture list. The developer creates two worktrees, each with its own branch, and lets a coding agent work in each. He starts the development server on a different port per worktree.
The bug fix is finished first, reviewed and merged. The registration page follows later and picks up the current state via rebase. The two agents never got in each other's way, and the main folder stayed clean throughout. Afterwards the developer removes both worktrees again.
Related Page
Git (Version Control)Frequently Asked Questions About Git Worktree
A branch is a line of development in the history. A worktree is a working folder in which a branch is checked out. Without worktrees you only ever see one branch at a time; with worktrees you see several side by side in different folders.
Usually yes. Folders such as node_modules live in the working folder and are not shared. Depending on the project, separate environment files and ports are needed too. Some tools automate these steps when a worktree is created.
Because several agents in the same folder would overwrite each other's files. With one worktree each, every agent works in isolation on its branch, tests run on a clean state, and results are merged in a controlled way at the end.
More Terms Starting with "G"
GEO (Generative Engine Optimization)
GEO is the optimization of content to appear and be cited in AI-generated search answers (Google AI ...
Google Business Profile
Google Business Profile (formerly: Google My Business) is the free Google tool for managing your loc...
Google Search Console
Google Search Console (GSC) is a free tool from Google that shows website operators how their pages ...
Ready for Your Project?
Apply this knowledge to your website — DLM Digital will help you.