my-git

Worktree

English 中文

git worktree allows the same repository to check out multiple branches into different directories simultaneously.

Best for people who need to process multiple tasks in parallel, especially for hotfixes, long-term branch maintenance, and parallel development by AI Agents.

Suitable Scenarios

Basic Commands

Create a new worktree:

git worktree add ../project-hotfix -b hotfix/urgent-fix

View worktrees:

git worktree list

Remove a worktree:

git worktree remove ../project-hotfix

Clean up invalid records:

git worktree prune

Difference from stash

stash is suitable for temporarily saving current unfinished changes.

worktree is suitable for keeping multiple complete workspaces simultaneously.

If you frequently switch between multiple branches within a day, worktree is usually clearer than stash.

AI Programming Scenarios

Avoid having multiple AI Agents write to the same workspace.

Recommended:

git worktree add ../repo-agent-a -b ai/task-a
git worktree add ../repo-agent-b -b ai/task-b

One branch and one directory per Agent, eventually consolidated and merged by humans.

Further Reading