Skip to content
OVEX TECH
Education & E-Learning

Boost Productivity: Use Git Worktrees in Agentic Coding

Boost Productivity: Use Git Worktrees in Agentic Coding

Mastering Agentic Coding with Git Worktrees

Git worktrees are a powerful feature that allows developers to manage multiple branches and tasks simultaneously. While beneficial in standard development workflows, their utility is amplified in agentic coding environments. This guide will show you how to leverage Git worktrees to work on multiple features concurrently with AI coding assistants, significantly speeding up your development process.

What You’ll Learn

This tutorial will guide you through the practical application of Git worktrees within an agentic coding workflow. You will learn how to initiate and manage multiple AI-driven coding tasks in parallel, effectively utilizing idle time while one agent is processing a request, and how this strategy can save considerable development time.

Prerequisites

  • Basic understanding of Git and version control.
  • Familiarity with AI coding assistants (e.g., Claude Code, Gemini CLI).
  • An existing Git project where you can experiment.

Step-by-Step Guide

Step 1: Set Up Your Environment with Multiple Worktrees

The core concept is to have separate Git worktrees, each dedicated to a different task or feature. This allows your AI coding assistant to work on one task while you (or another AI session) can initiate a new task in a different worktree without interference.

  1. Create Additional Worktrees: If you haven’t already, create new worktrees for your project. For example, if your main worktree is on the ‘main’ branch, you might create one for ‘feature-abc’ and another for ‘feature-xyz’.
    git worktree add ../feature-abc abc-branch
    git worktree add ../feature-xyz xyz-branch
  2. Open Separate Terminal Windows: For each worktree, open a dedicated terminal window. Navigate into the directory corresponding to each worktree.
    cd path/to/your/project/feature-abc
    cd path/to/your/project/feature-xyz

Expert Tip: Naming your worktree directories and branches descriptively (e.g., `feature-user-auth`, `bugfix-login-issue`) will help you keep track of ongoing tasks.

Step 2: Initiate the First Agentic Task

In one of your terminal windows (e.g., the ‘feature-abc’ worktree), you can now start an AI coding task. This could involve asking the AI to flesh out a new component, write a specific function, or implement a part of a feature.

  1. Define the Task: Formulate your prompt for the AI coding assistant. For instance, you might ask it to create an ‘About Us’ page.
    # Example prompt for an AI assistant within the 'feature-abc' terminal
    # Ask Claude to create an about page.
    Send prompt: Create a detailed 'About Us' page with company mission and team introduction sections.
  2. Submit the Prompt: Send the request to your AI assistant. The AI will begin processing this task, which may take some time depending on complexity.

Step 3: Switch and Initiate a Second Agentic Task

While the first AI task is running, you can switch to your other terminal window (e.g., the ‘feature-xyz’ worktree) and start a completely different task. This is where the time-saving benefit of worktrees becomes apparent.

  1. Switch to the Second Worktree: Navigate to the directory of your second worktree if you haven’t already.
  2. Define and Submit the Second Task: Formulate a new prompt for a different feature or task. For example, you could ask the AI to create a ‘Contact Us’ page with a form.
    # Example prompt for an AI assistant within the 'feature-xyz' terminal
    # Ask Claude to make a contact page with a contact form.
    Send prompt: Generate a 'Contact Us' page including a form with fields for name, email, subject, and message, along with a submit button.
  3. Run Concurrently: Both AI assistants are now working on separate tasks in their respective worktrees simultaneously.

Warning: Be mindful of the complexity of tasks. If tasks are too complex or resource-intensive, running too many concurrently might still lead to performance issues on your machine or within the AI’s processing capabilities.

Step 4: Manage and Review Progress

As the AI agents complete their tasks, you will have code generated in separate worktrees. You can then review, refine, and integrate these changes.

  1. Monitor AI Output: Keep an eye on the output from both AI sessions.
  2. Review Code Locally: Once a task is complete in a worktree, you can review the generated code directly within that worktree’s directory. Make any necessary manual adjustments.
  3. Commit and Push: After reviewing and finalizing the code for a feature, commit the changes within that specific worktree and push them to its corresponding branch on your remote repository (e.g., GitHub).
    # Example commands within the 'feature-abc' worktree
    git add .
    git commit -m "feat: Implement About Us page"
    git push origin abc-branch
  4. Merge Changes: Once pushed, you can create a pull request and merge the feature branch into your main branch, following your standard Git workflow. Repeat this process for each worktree/feature.

Expert Note: While you can run two or three worktrees effectively, managing five or six simultaneously can become complex and increase the likelihood of manual merge conflicts. Stick to a number that feels manageable for your workflow.

Step 5: Automate Worktree Creation (Advanced)

For an even more streamlined agentic workflow, you can train your AI assistant to autonomously create new worktrees and branches when it starts a new feature.

  1. Custom Commands: Some AI coding tools allow for custom slash commands or configuration. You could create a command that instructs the AI to:
    • Detect the start of a new feature request.
    • Automatically create a new Git worktree.
    • Create a new branch for that worktree.
    • Begin working on the feature within the new worktree.
  2. Triggering the Command: With such a setup, you would simply provide the feature specification, and the AI handles the Git infrastructure setup for you.

Further Learning: If you’re interested in this advanced automation, explore resources on setting up agentic workflows with custom commands, sub-agents, and integrated Git/GitHub management.

Conclusion

By strategically employing Git worktrees, you can transform your agentic coding sessions from sequential tasks into parallel processes. This not only saves significant waiting time but also allows for more efficient iteration and development. Whether you’re using AI assistants or working solo, mastering Git worktrees is a valuable skill for any developer looking to boost productivity.


Source: Git Worktrees Tutorial #5 – Worktrees in Agentic Coding Workflows (YouTube)

Leave a Reply

Your email address will not be published. Required fields are marked *

Written by

John Digweed

1,794 articles

Life-long learner.