Use Git Worktree for AI Assistant Coding
If you're using AI coding assistants like Claude Code, GitHub Copilot, or Cursor, there's a powerful git feature you're probably overlooking: git worktree.
The Problem with AI-Assisted Development
When working with AI assistants, you often find yourself in these situations:
- The AI is making changes to your codebase while you want to continue working
- You need to test AI-generated code without risking your current work
- Multiple AI sessions are working on different features simultaneously
- You want to compare AI implementations against your own
Traditionally, you'd either wait, stash your changes, or juggle multiple clones of your repository. All of these options are painful.
Enter Git Worktree
Git worktree lets you check out multiple branches of the same repository into different directories simultaneously. Each worktree shares the same .git directory, so they're all connected.
# Create a new worktree for AI work
git worktree add ../my-project-ai-feature feature-branch
# Now you have:
# /my-project <- your main working directory
# /my-project-ai-feature <- AI assistant's playground
Why This Is Perfect for AI Coding
1. Parallel Workflows
Point your AI assistant at the worktree directory while you continue working in the main directory. No more waiting. No conflicts.
# Terminal 1: You working in main
cd my-project
vim src/important-file.ts
# Terminal 2: AI assistant working in worktree
cd my-project-ai-feature
claude-code "implement the new auth system"
2. Safe Experimentation
AI assistants can make sweeping changes. With worktrees, those changes are isolated. If the AI creates a mess, you simply:
git worktree remove ../my-project-ai-feature
Your main directory remains untouched.
3. Easy Comparison
Want to see what the AI did vs. your approach? Both directories exist simultaneously:
diff -r my-project/src my-project-ai-feature/src
Or open both in your IDE side-by-side.
4. Multiple AI Agents
Running multiple AI sessions on different tasks? Create multiple worktrees:
git worktree add ../project-auth auth-feature
git worktree add ../project-api api-refactor
git worktree add ../project-tests test-coverage
Each AI agent works independently without stepping on each other.
5. Shared Git History
Unlike separate clones, worktrees share the git database. Commits made in any worktree are immediately visible to all others. Merging AI work back into main is seamless.
Practical Workflow
Here's a typical workflow I use:
# 1. Create a feature branch and worktree for AI
git checkout -b feature/ai-implementation
git worktree add ../project-ai feature/ai-implementation
# 2. Point AI assistant at the worktree
cd ../project-ai
# Start AI coding session here
# 3. Continue your own work in main
cd ../my-project
git checkout main
# Work normally
# 4. Review AI changes
cd ../project-ai
git diff main
# Run tests, review code
# 5. Merge when satisfied
cd ../my-project
git merge feature/ai-implementation
# 6. Clean up
git worktree remove ../project-ai
git branch -d feature/ai-implementation
Tips and Tricks
List all worktrees:
git worktree list
Prune stale worktrees:
git worktree prune
Lock a worktree to prevent accidental deletion:
git worktree lock ../project-ai
The Bottom Line
Git worktree transforms how you can work with AI coding assistants. Instead of a sequential, blocking workflow, you get parallel, isolated development environments that share the same repository.
The AI can experiment freely. You can work uninterrupted. And when it's time to integrate, everything is already in the same git repo.
If you're serious about AI-assisted development, git worktree isn't just nice to have—it's essential.
Start small: next time you're about to let an AI assistant make changes, create a worktree first. You'll wonder how you ever worked without it.