Step 4 / 6 ยท Worktree

Worktree: two flower beds at the same time

A worktree is another folder for the same project, open on another branch.

Illustration: two flower beds connected to the same photo cabinet; flowers grow in one while the fence is repaired in the other
In the garden

While you are working among the flowers, the fence breaks and needs to be fixed right away. You open a second flower bed: keep working on the flowers in one, fix the fence in the other.

In practice

git worktree add creates a second working folder connected to the same repository. Both folders share the same history (the same .git folder), but each has its own staging area and branch.

It is great for handling an urgent task without dropping what you are doing, and for letting an AI agent work in one folder while you work in the other.

Commands

git worktree add ../garden-main main  # open main in another folder
cd ../garden-main  # go there
git commit -am "Fix the fence"  # commit C on main

Tip: The same branch cannot be open in two folders at the same time.

What the history looks like

AfirstBpathCfenceDflowers 1Eflowers 2mainflowers