Every chat in 1Code runs in its own git worktree. This means agents can make changes, run tests, and commit code without touching your main branch or interfering with other chats.Documentation Index
Fetch the complete documentation index at: https://mintlify.com/21st-dev/1code/llms.txt
Use this file to discover all available pages before exploring further.
Why It Matters
Running AI agents directly in your main repository is risky:- Accidental commits to main - One wrong command and your production code is altered
- Conflicts between sessions - Running multiple agents at once causes file conflicts
- Lost work - If an agent breaks something, you lose your local changes
- No isolation - Can’t compare different approaches side-by-side
How It Works
Git Worktrees Explained
A git worktree is a separate working directory linked to your repository. It has:- Its own branch
- Its own file state
- Shared git history with the main repo
- No risk of conflicts with other worktrees
Automatic Worktree Creation
When you start a new chat:- 1Code creates a new worktree in
../worktrees/chat-<id>/ - A branch
chat-<id>is created from your current branch - The agent runs in this isolated directory
- All file changes stay in the worktree
src/main/lib/trpc/routers/worktree-config.ts:5, which reads from .cursor/worktrees.json or .1code/worktree.json.
Worktree Lifecycle
Agent Works
All file edits, commits, and git operations happen in the worktree. Your main repo is untouched.
Setup Scripts
Configure worktree setup commands in.1code/worktree.json:
- Install dependencies
- Build the project
- Set up databases or services
- Copy environment files
Worktree Directory Structure
Benefits
1. Branch Safety
Worktrees prevent accidental commits tomain:
- Each chat has its own branch
- Agents can’t access your main branch
- Merging to main is explicit and manual
2. Parallel Execution
Run multiple agents simultaneously:- Chat 1: Refactoring the API
- Chat 2: Adding tests
- Chat 3: Fixing a bug
3. Safe Experimentation
Try Different Approaches
Fork a chat to test two solutions side-by-side. Each runs in its own worktree.
Rollback Easily
If an agent breaks something, just delete the worktree. Your main repo is safe.
Compare Results
Review diffs from multiple chats to pick the best solution.
Preserve History
Even after archiving a chat, the branch remains for future reference.
4. Clean Diffs
Because each chat starts from a known state, diffs are clean:- Only shows changes made by the agent
- No unrelated modifications
- Easy to review and merge
Merging Changes to Main
When you’re happy with the agent’s work: Option 1: Create a PR- Click “Create PR” in the changes sidebar
- Review the diff on GitHub
- Merge when ready
- Switch to main branch
- Run
git merge chat-<id> - Resolve conflicts if any
- Push to remote
- Open commit history
- Right-click a commit
- Select “Cherry-pick to main”
Configuration
Custom Worktree Location
By default, worktrees are created in../worktrees/. Change this in settings:
Shared Worktree Directory
Use a shared directory for all projects:<project>-chat-<id> to avoid collisions.
Cleanup Policy
Configure when worktrees are deleted:- On archive: Delete immediately (default)
- On exit: Delete when 1Code closes
- Manual: Keep until explicitly deleted
Troubleshooting
Worktree creation failed
Worktree creation failed
Cause: Git version too old or insufficient permissions.Fix:
- Update git to 2.35+
- Ensure write access to the worktree directory
- Check disk space
Setup scripts not running
Setup scripts not running
Cause: Scripts are not executable or failed to run.Fix:
- Check
.1code/worktree.jsonsyntax - Make scripts executable:
chmod +x script.sh - View logs in Settings → Debug → Worktree Logs
Changes not showing in main repo
Changes not showing in main repo
Cause: Worktrees are isolated by design.Fix:
- This is expected behavior
- Merge the chat branch to main when ready
- Or create a PR to review changes first
Disk space filling up
Disk space filling up
Cause: Too many worktrees or large dependencies.Fix:
- Archive old chats to delete worktrees
- Use symbolic links for node_modules
- Configure cleanup policy to “On exit”
Best Practices
Review Before Merging
Always review the diff and commit history before merging a chat branch to main.
Use Descriptive Branch Names
Rename chat branches to describe their purpose:
git branch -m chat-abc123 feature/add-authClean Up Regularly
Archive chats you’re not using to free up disk space.
Test in the Worktree
Run tests in the worktree before merging to catch issues early.
Related Features
- Background Agents - Cloud sandboxes use the same isolation model
- Visual UI - Review worktree changes with diff previews
- Multi-Agent Support - Run different agents in parallel worktrees