--worktree starts M31A in a fresh Git worktree so the agent can change files without touching your current checkout. This tutorial runs two isolated tasks and then merges them.
WARNING
Worktrees are experimental and disabled by default. The flag is rejected during argument validation until experimental.worktrees is enabled.
Prerequisites
- A Git repository with a clean or predictable working tree.
- M31A installed and authenticated.
- Git available on
PATH.
1. Enable the feature
Add to ~/.m31a/settings.json or <project>/.m31a/settings.json:
{
"experimental": {"worktrees": true}
}Verify that the setting is visible with /settings. Without it, starting with --worktree is rejected during argument validation.
2. Understand what is created
packages/cli/src/utils/worktreeSetup.ts resolves the repository root, records the current HEAD SHA, and calls WorktreeService.setup(). The service in packages/core/src/services/worktreeService.ts:
- captures the base commit with
git rev-parse HEAD; - creates
.m31a/worktrees/<name>; - adds it with
git worktree add ... -b worktree-<name>; - changes the process working directory into it.
If you omit a name, M31A generates one from a timestamp and a short random suffix. Each worktree has its own branch named worktree-<name>.
3. Launch two isolated sessions
From the main checkout, start the first session in one terminal:
m31a --worktree docs-polish \
-p "Update the README to describe the build commands accurately. Commit the change with a conventional commit message."In a second terminal, start another:
m31a --worktree test-coverage \
-p "Add unit tests for the parser utility. Commit the change with a conventional commit message."Both sessions operate in their own directories and branches. Your main checkout is untouched. Use --skip-trust in each invocation if you want to skip interactive trust prompts.
INFO
The two sessions share the same Git object database and repository-level configuration. Do not run commands that rewrite shared refs from both worktrees at once.
4. Verify isolation
From the main checkout:
git status --short
git worktree list
git branch --list 'worktree-*'git status should show no changes from the agents. The worktree list should show .m31a/worktrees/docs-polish and .m31a/worktrees/test-coverage.
Inspect one result without leaving your terminal:
git -C .m31a/worktrees/docs-polish log --oneline -3
git -C .m31a/worktrees/test-coverage status --short5. Review and merge
Review each diff before merging:
git -C .m31a/worktrees/docs-polish diff HEAD~1 --stat
git -C .m31a/worktrees/test-coverage diff HEAD~1 --statThen merge from the main checkout:
git merge --no-ff worktree-docs-polish
git merge --no-ff worktree-test-coverageResolve any conflicts as you would with normal branches. Push or open a pull request from the main branch after both merges are verified.
6. Clean up
Once a worktree is no longer needed:
git worktree remove .m31a/worktrees/docs-polish
git worktree remove .m31a/worktrees/test-coverage
git branch -d worktree-docs-polish worktree-test-coverageWorktreeService.maybeCleanup() contains logic to remove an unmodified worktree, but the supported workflow is to remove worktrees explicitly after reviewing them.
7. Session considerations
Session records are keyed by project path, so each worktree gets its own project temp directory and its own chat history. Use --list-sessions inside a worktree to see its sessions, and Session storage for where those files live.
Checkpoints use a shadow Git repository rooted at the project; if you use checkpointing, enable it consistently across sessions:
{
"general": {"checkpointing": {"enabled": true}}
}Troubleshooting
| Symptom | Resolution |
|---|---|
| Flag rejected at startup | Enable experimental.worktrees. |
worktree add fails | The name already exists or the branch is checked out elsewhere. |
| Agent edits the wrong tree | Check the working directory shown in the TUI. |
| Merge conflicts | Normal Git resolution; the branches are independent. |
| Stale worktree | Use git worktree prune after manual directory removal. |
Next steps
- Read Worktrees for the lifecycle.
- Read Session storage for per-worktree records.
- Automate isolated reviews with Headless CI.