Skip to main content

Git worktrees–A first step towards a multi-agent development workflow

As AI coding assistants become more sophisticated, we're approaching a future where multiple agents might work on different parts of your codebase simultaneously. But there's a challenge: how do you let multiple processes work on the same repository without constantly stepping on each other's toes?

One solution is to have agents work on dedicated machines like GitHub Copilot Agent does in a GitHub Codespace. But what if you want to have multiple agents working on your local machine?

Enter git worktrees – a powerful Git feature that's been hiding in plain sight since 2015, and the perfect foundation for multi-agent development workflows.

What are Git worktrees?

Git worktrees allow you to check out multiple branches from the same repository simultaneously, each in its own directory. Think of it as having multiple working directories all sharing the same Git history, but each can be on a different branch.

Here's the key insight: while the working directories are separate, they all share the same .git directory.

This means:

  • No duplication of Git history
  • Minimal disk space overhead
  • Lightning-fast branch creation
  • Perfect isolation between workspaces

Enter multi-agent development

When you have multiple AI agents working on your codebase:

  • Agent A might be refactoring the authentication system
  • Agent B could be writing tests for the API layer
  • Agent C might be experimenting with a new UI component

With worktrees, each agent gets its own isolated workspace. They can run builds, execute tests, and make changes without interfering with each other – all while sharing the same underlying repository.

Important is to use a good directory and naming structure. Here's one way to do it:

~/projects/
├── my-app/                 # Main worktree (main branch)
├── my-app-agent-1/         # Agent 1's workspace
├── my-app-agent-2/         # Agent 2's workspace
└── my-app-hotfix/          # Quick hotfix workspace

Getting started

Let's say you're working on the main branch and need a separate workspace:

# Create a new worktree for a feature branch
git worktree add ../my-repo-feature feature/new-auth

# Create a worktree with a new branch
git worktree add -b feature/api-refactor ../my-repo-api

This creates a new directory at ../my-repo-feature with the feature/new-auth branch checked out.

You can see the list of created worktrees:

git worktree list

Output:

/home/user/projects/my-repo         abc123d [main]
/home/user/projects/my-repo-feature def456e [feature/new-auth]
/home/user/projects/my-repo-api     ghi789f [feature/api-refactor]

When you're done with a worktree:

# Remove the worktree
git worktree remove ../my-repo-feature

# Or if you've already deleted the directory
git worktree prune

Using worktrees in VS Code

VS Code recently added support for git worktrees., You can create a new worktree directly from the UI:

And of course, also delete it, when you no longer need it:

 

Remark: Although Visual Studio does not have native support for Git worktrees, you can use this feature through the Git WorkTree extension available on the Visual Studio Marketplace.

More information

Git - git-worktree Documentation

Git WorkTree - Visual Studio Marketplace

Popular posts from this blog

Azure DevOps/ GitHub emoji

I’m really bad at remembering emoji’s. So here is cheat sheet with all emoji’s that can be used in tools that support the github emoji markdown markup: All credits go to rcaviers who created this list.

Podman– Command execution failed with exit code 125

After updating WSL on one of the developer machines, Podman failed to work. When we took a look through Podman Desktop, we noticed that Podman had stopped running and returned the following error message: Error: Command execution failed with exit code 125 Here are the steps we tried to fix the issue: We started by running podman info to get some extra details on what could be wrong: >podman info OS: windows/amd64 provider: wsl version: 5.3.1 Cannot connect to Podman. Please verify your connection to the Linux system using `podman system connection list`, or try `podman machine init` and `podman machine start` to manage a new Linux VM Error: unable to connect to Podman socket: failed to connect: dial tcp 127.0.0.1:2655: connectex: No connection could be made because the target machine actively refused it. That makes sense as the podman VM was not running. Let’s check the VM: >podman machine list NAME         ...

Cleaner switch expressions with pattern matching in C#

Ever find yourself mapping multiple string values to the same result? Being a C# developer for a long time, I sometimes forget that the C# has evolved so I still dare to chain case labels or reach for a dictionary. Of course with pattern matching this is no longer necessary. With pattern matching, you can express things inline, declaratively, and with zero repetition. A small example I was working on a small script that should invoke different actions depending on the environment. As our developers were using different variations for the same environment e.g.  "tst" alongside "test" , "prd" alongside "prod" .  We asked to streamline this a long time ago, but as these things happen, we still see variations in the wild. This brought me to the following code that is a perfect example for pattern matching: The or keyword here is a logical pattern combinator , not a boolean operator. It matches if either of the specified pattern...