Skip to content

--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:

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:

  1. captures the base commit with git rev-parse HEAD;
  2. creates .m31a/worktrees/<name>;
  3. adds it with git worktree add ... -b worktree-<name>;
  4. 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:

bash
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:

bash
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:

bash
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:

bash
git -C .m31a/worktrees/docs-polish log --oneline -3
git -C .m31a/worktrees/test-coverage status --short

5. Review and merge ​

Review each diff before merging:

bash
git -C .m31a/worktrees/docs-polish diff HEAD~1 --stat
git -C .m31a/worktrees/test-coverage diff HEAD~1 --stat

Then merge from the main checkout:

bash
git merge --no-ff worktree-docs-polish
git merge --no-ff worktree-test-coverage

Resolve 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:

bash
git worktree remove .m31a/worktrees/docs-polish
git worktree remove .m31a/worktrees/test-coverage
git branch -d worktree-docs-polish worktree-test-coverage

WorktreeService.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:

json
{
  "general": {"checkpointing": {"enabled": true}}
}

Troubleshooting ​

SymptomResolution
Flag rejected at startupEnable experimental.worktrees.
worktree add failsThe name already exists or the branch is checked out elsewhere.
Agent edits the wrong treeCheck the working directory shown in the TUI.
Merge conflictsNormal Git resolution; the branches are independent.
Stale worktreeUse git worktree prune after manual directory removal.

Next steps ​

Built with VitePress. Powered by NVIDIA NIM.