Claude Code Now has Native Worktree Support (and It's Good)

BBetter Stack
Computing/SoftwareInternet Technology

Transcript

00:00:00Claude Code has received an insane amount
00:00:02of updates recently from Claude Desktop's SSH support,
00:00:05app previews, and to GitHub integration
00:00:07to simple mode in the terminal,
00:00:09which strips out skills, plugins, and custom tools,
00:00:11making it easy for basic tasks.
00:00:13But the biggest update by far is native work tree support,
00:00:16meaning your agents can work on multiple features
00:00:18or bug fixes in isolation
00:00:20without you having to manually configure anything.
00:00:23But was this released too soon,
00:00:25with many developers already having basic issues?
00:00:28Hit subscribe, and let's get into it.
00:00:30Using Git's work trees with coding agents is nothing new.
00:00:33In fact, Claude Desktop has had it for a while,
00:00:35and there are many plugins or tools that support it,
00:00:37like Claude Squad, which we've already made a video about.
00:00:41But one of the reasons I personally haven't done it
00:00:43is because doing it manually can add mental load,
00:00:46and hopefully this new feature in Claude Code
00:00:49will make it a lot easier for me.
00:00:50Let's try it.
00:00:51So here is a basic to-dos app built by Claude in React.
00:00:54For testing purposes, I wouldn't actually build this app
00:00:56for any other reason than to test.
00:00:58But anyway, I'd like to add a dark mode toggle
00:01:01because this is blinding my eyes.
00:01:02I'd also like to add the ability to save this
00:01:04in local storage because if I refresh, it doesn't save.
00:01:07And I'd like to add the ability to edit to-dos.
00:01:10Now, usually I'd put all those features into one prompt,
00:01:12so dark mode toggle, local storage,
00:01:14and the ability to edit to-dos.
00:01:16The problem with that is if one of these features had a bug,
00:01:20then I'd have to work on the whole thing,
00:01:22so delay the other features from being released
00:01:25because everything is on the same PR or the same branch.
00:01:28But with work trees,
00:01:28I can work on these features in separate branches
00:01:31at the same time on my local machine
00:01:33without having to do any manual setup.
00:01:35And if there is an issue with one of these features,
00:01:37I can push the other two features
00:01:39and work on that one on its own branch.
00:01:41So let me show you how to do that.
00:01:42So if I do Claude Code with the W flag,
00:01:45then that will give me a work tree
00:01:47with a random name chosen by Claude.
00:01:49Wait, Lacan?
00:01:50Okay.
00:01:52But if I wanted to give my work tree a name,
00:01:54I'd give it a name after the W flag,
00:01:56which I'll put it under here.
00:01:58This gives me a new work tree over here,
00:02:00and it also creates a new branch,
00:02:02but doesn't delete the previous work tree that we created.
00:02:05So keep that in mind
00:02:06if you end up making a lot of temporary work trees.
00:02:08And in the .claud directory of the project under work trees,
00:02:11we can look at our work tree
00:02:13and see all the code files associated with it.
00:02:15Note, work trees only work
00:02:16if your project has git initialize
00:02:18and you've made at least one commit.
00:02:20Now I can go ahead and implement all the features
00:02:22to this project in different work trees.
00:02:24So now all the three features have finished
00:02:26and we can see here that the dark mode toggle works.
00:02:29In this one, there's no dark mode toggle,
00:02:31but I can edit the to-do.
00:02:32So I'll add a new one and change it here.
00:02:35And in the local storage one, there seems to be an issue,
00:02:38but this is the benefit of work trees
00:02:40because I can see that the other two work well.
00:02:43So I could commit those or create a pull request
00:02:44and I could focus on this one
00:02:46and I could use a dedicated Claude Code session
00:02:48to fix this issue.
00:02:49And when I'm done with the work tree,
00:02:51I can either keep it so I can go back to it later
00:02:53or remove it from my system.
00:02:54If I decided to keep it,
00:02:55I could go back to the exact same work tree
00:02:58if I specify the name, as you can see here.
00:03:00I could even resume sessions from my current work tree
00:03:03or press Control + W to see all the sessions in this project.
00:03:06Although I'm not quite sure
00:03:07how it works out the current session,
00:03:08since right now I'm on the main branch.
00:03:11Speaking of current session,
00:03:12if you wanted to work from the work tree directory,
00:03:15you could either navigate directly to it
00:03:16using cd.claud work trees and so on,
00:03:19or you could run work trees with the tmux flag,
00:03:21which will take you straight to that directory.
00:03:23And if I opened up a new split pane,
00:03:26you can see I'm in that work tree
00:03:27and I can navigate through the work tree specific files.
00:03:30But the coolest thing about work trees
00:03:31is that they support sub-agents,
00:03:33meaning I can get different sub-agents
00:03:35to work on multiple features in different branches
00:03:38in the same Claude Code session.
00:03:40And if you want work tree support in custom sub-agents,
00:03:42so a custom sub-agent will automatically spin up a work tree,
00:03:45all you have to do is add this isolation work tree
00:03:48in the front matter and Claude will take care of the rest.
00:03:50Overall, I'm really impressed with this feature
00:03:52and it's something that I'm definitely
00:03:54be going to using more of,
00:03:55particularly with the new work tree hooks
00:03:57that allow you to use work trees without git,
00:04:00so SVN, jiu-jitsu, et cetera,
00:04:02and also to run scripts automatically
00:04:04when you create a new work tree.
00:04:05But unfortunately, it seems a lot of people
00:04:08have been having issues with work trees
00:04:09since the latest Claude Code updates
00:04:11because of what looks like a broken feature flag.
00:04:13Luckily, you can get Claude Code to fix its own binary
00:04:17as a temporary measure,
00:04:18but hopefully the team will have a proper fix later on
00:04:21so that more people can experience this awesome feature.
00:04:24Speaking of awesome features,
00:04:25if you're a React developer and want a CLI tool
00:04:28to automatically detect React anti-patterns,
00:04:31then check out this video from Andris
00:04:33that goes through React Doctor.

Key Takeaway

Claude Code's native worktree support revolutionizes AI-assisted development by enabling seamless, parallel feature implementation in isolated environments without manual Git configuration.

Highlights

Claude Code's new native worktree support allows agents to work on multiple features or bug fixes in isolation.

The -w flag enables the creation of named or randomized worktrees, automatically handling branch creation and environment setup.

Worktrees resolve the 'monolithic PR' problem by allowing developers to push stable features while isolating buggy ones.

Advanced features include sub-agent support where multiple AI agents work on different branches simultaneously.

Integration with tools like tmux allows for seamless navigation into specific worktree directories directly from the terminal.

New 'isolation worktree' front matter allows custom sub-agents to automatically spin up their own isolated environments.

Despite being a powerful feature, some users have reported initial bugs related to feature flags in the latest update.

Timeline

Introduction and Recent Claude Code Updates

The speaker introduces a wave of significant updates to Claude Code, including SSH support for Claude Desktop, app previews, and GitHub integration. A new 'simple mode' is highlighted for its ability to strip out complex tools for basic terminal tasks. However, the focal point of the video is the introduction of native worktree support for coding agents. This feature aims to allow AI agents to work on multiple features or bug fixes in isolation without manual user configuration. The introduction sets the stage by questioning if the feature was released too early given some reported developer issues.

The Problem with Traditional Agent Workflows

The speaker explains that while using Git worktrees with agents isn't entirely new, manual setup often adds a significant mental load. Using a React to-do app as an example, the speaker outlines the risk of bundling multiple features like dark mode, local storage, and edit functionality into a single prompt. If one feature contains a bug, it delays the release of all other features because they reside on the same branch or Pull Request. Native worktree support solves this by allowing these features to be developed in separate branches simultaneously. This isolation ensures that stable code can be pushed even if one specific feature requires further debugging.

Demonstrating Worktree Creation and Management

The technical implementation is demonstrated using the Claude Code '-w' flag to generate a worktree with either a random or specified name. This process automatically creates a new branch and a dedicated directory under the .claud folder where all associated code files are stored. The speaker notes that Git must be initialized with at least one commit for this functionality to work correctly. After implementing features across different worktrees, the speaker shows how the dark mode and edit features succeeded while local storage failed. This specific failure highlights the benefit of isolation, as the user can focus a dedicated Claude session on the broken branch without affecting the working code.

Advanced Features: Sub-agents and Navigation

The video delves into advanced navigation and automation features, such as using Control + W to view all active sessions within a project. For developers using terminal multiplexers, the 'tmux' flag allows for immediate navigation into the specific worktree directory within a new split pane. Perhaps the most impressive capability discussed is the support for sub-agents, which can handle multiple features across different branches within a single Claude Code session. Users can even automate this by adding 'isolation: worktree' to the front matter of custom sub-agents. The speaker also mentions upcoming 'worktree hooks' that will allow for scripts to run automatically upon worktree creation and support for non-Git systems like SVN.

Troubleshooting and Closing Thoughts

The speaker addresses current stability issues, noting that many developers have encountered bugs due to a broken feature flag in recent updates. Interestingly, a temporary workaround involves using Claude Code itself to fix its own binary, showcasing the tool's self-healing potential. Despite these early hiccups, the speaker expresses strong impressions of the feature and its utility for complex development workflows. The brief concludes with a recommendation for a tool called React Doctor for developers interested in detecting anti-patterns. Overall, the section reinforces that while the feature is powerful, users should be prepared for minor technical adjustments as the tool matures.

Community Posts

No posts yet. Be the first to write about this video!

Write about this video