|
| 1 | +# Mastering Git Worktrees: Parallel Development with AI Agents |
| 2 | + |
| 3 | +## The Context Switching Nightmare |
| 4 | + |
| 5 | +We've all been there. You're deep in the zone, refactoring a complex component on `feature-branch-A`. Suddenly, a critical bug report comes in. |
| 6 | + |
| 7 | +**The Old Way:** |
| 8 | +1. `git stash` (Hope you remember what was in there). |
| 9 | +2. `git checkout main`. |
| 10 | +3. `git pull`. |
| 11 | +4. `git checkout -b hotfix-critical-bug`. |
| 12 | +5. `npm install` (Wait 2 minutes because `package-lock.json` changed). |
| 13 | +6. Fix the bug. |
| 14 | +7. Switch back, `npm install` *again*, `git stash pop`. |
| 15 | +8. Where was I? |
| 16 | + |
| 17 | +**The Worktree Way:** |
| 18 | +1. Go to a new folder. |
| 19 | +2. Fix the bug. |
| 20 | +3. Close the folder. |
| 21 | + |
| 22 | +## What are Git Worktrees? |
| 23 | + |
| 24 | +Git Worktrees allow you to have **multiple branches of the same repository checked out at the same time** in different directories. |
| 25 | + |
| 26 | +Instead of swapping the files in your current directory (which `git checkout` does), a worktree creates a *new* directory linked to the same `.git` history but with a different branch checked out. |
| 27 | + |
| 28 | +### Basic Commands |
| 29 | + |
| 30 | +```bash |
| 31 | +# Add a new worktree for a feature branch |
| 32 | +git worktree add ../my-app-feature feature-branch |
| 33 | + |
| 34 | +# List active worktrees |
| 35 | +git worktree list |
| 36 | + |
| 37 | +# Remove a worktree when done |
| 38 | +git worktree remove ../my-app-feature |
| 39 | +``` |
| 40 | + |
| 41 | +## The Power of Parallelism |
| 42 | + |
| 43 | +With worktrees, you can: |
| 44 | +1. **Run different versions of your app simultaneously.** Have `localhost:3000` running `main` (for reference) and `localhost:3001` running your `feature` (for dev). |
| 45 | +2. **Zero `npm install` fatigue.** Each worktree has its own `node_modules`. Switching context is instant because you aren't actually switching *files*, just windows. |
| 46 | + |
| 47 | +## Worktrees + AI Agents: The Multi-Agent Workflow |
| 48 | + |
| 49 | +This is where it gets sci-fi. |
| 50 | + |
| 51 | +If you are using LLM agents like Gemini CLI, Devin (does anyone remember Devin???), or GitHub Copilot Workspace, they usually lock your terminal or editor while working. |
| 52 | + |
| 53 | +**With Worktrees, you can act as a Manager for multiple AI Agents working in parallel.** |
| 54 | + |
| 55 | +### The Setup |
| 56 | + |
| 57 | +Imagine a project structure like this: |
| 58 | +```text |
| 59 | +/workhammer |
| 60 | + /main (Your "stable" repo) |
| 61 | + /feat-ui (Worktree: Agent 1 refactoring CSS) |
| 62 | + /feat-backend (Worktree: Agent 2 migrating database) |
| 63 | + /fix-auth (Worktree: Agent 3 fixing login bug) |
| 64 | +``` |
| 65 | + |
| 66 | +### The Workflow |
| 67 | + |
| 68 | +1. **Delegate Task A:** Open a terminal in `/feat-ui`. Tell the AI: *"Refactor the sidebar to use Tailwind Grid."* Let it run. |
| 69 | +2. **Delegate Task B:** Open a terminal in `/feat-backend`. Tell the AI: *"Update the Prisma schema for the new User model."* Let it run. |
| 70 | +3. **Review:** While they work, you sit in `/main` and review Pull Requests or plan the next sprint. |
| 71 | + |
| 72 | +Because worktrees are isolated directories, the Agents don't step on each other's toes. They don't fight over `git.lock` files or overwrite each other's uncommitted changes. |
| 73 | + |
| 74 | +## Best Practices |
| 75 | + |
| 76 | +* **Gitignore:** Make sure your `.gitignore` is solid. You don't want build artifacts from one tree leaking (though usually, they are separated by folders anyway). |
| 77 | +* **Disk Space:** Remember, `node_modules` is heavy. 5 worktrees = 5x the `node_modules` size. Prune your worktrees (`git worktree prune`) often. |
| 78 | +* **VS Code Profiles:** Use VS Code Workspaces to manage these multi-root setups easily. |
| 79 | + |
| 80 | +## Conclusion |
| 81 | + |
| 82 | +Git Worktrees are a developer superpower. Combined with AI agents, they transform you from a single-threaded coder into a parallel-processing technical lead. Stop context switching; start forking your environment. |
0 commit comments