by Scott Chacon

2 min read

Managing Multiple Claude Code Sessions Without Worktrees

With Claude Code's new lifecycle hooks, GitButler can now automatically sort simultaneous AI coding sessions into separate branches. Write three features at the same time, get three independent branches. No conflicts, no worktrees, no bootstrap headaches.

post-preview

One of the very cool things about using Claude Code for code generation is that you can easily run multiple instances of it at the same time, even in the same working directory.

However, Anthropic doesn’t suggest this because then you would commit everything into a single branch, or worst case, all in one commit at the end of your sessions. Instead, they suggest either separate checkouts or the use of git worktree.

The problem here is that you need to bootstrap each of the worktrees (npm install, build artifacts, etc) and worse than that, now you have created the fun new ability to create merge conflicts with yourself.

There's gotta be a better way!

Claude Code hooks and GitButler

Recently, Claude Code released a new version which includes lifecycle hooks, meaning that you can tell Claude to run an external command before certain key actions that it takes.

This means that now GitButler can be informed when files are changed or chats are completed. This allows us to take work generated in simultaneous sessions operating on a single working directory and place work generated in each one into multiple simultaneous branches.

Here's a quick (under 3 min) demo of writing three features on a Twitter clone at the same time and ending up with three independent branches which can be merged in any order:

Three sessions, three branches - that's the Chicago way.

How does this work?

GitButler has long had the ability to take multiple changes in a working directory and assign and commit them to multiple branches.

multiple simultaneous branches in one directory using the GitButler client

With Claude Code hooks, you can make Claude tell GitButler when a file is about to be edited and when it's done being edited. This call gives us a session id, so we can automatically create a new branch for every session we see and assign any work done by that session to the associated branch. When the chat is done, another Claude hook lets us know and we can then commit the work done in that completed chat.

Actually, we do two things. We do a quick commit that stores the prompt used to generate that change, then we spawn another background process that optionally uses AI to rewrite the commit message based on that prompt and the files changed.

This leaves you with one commit per chat round, and one branch per Claude Code session. You can of course then squash, reorder, split or un-commit this work if you want.

How can I try it?

The newest release of the GitButler client includes our new experimental CLI that has commands built for the Claude Code hooks. You can follow our help docs to easily set it up after installing our CLI.

Once the hooks are setup, everything should automatically work. If you have any problems or feedback, come shout at us in our Discord.

Scott Chacon

Written by Scott Chacon

Scott Chacon is a co-founder of GitHub and GitButler, where he builds innovative tools for modern version control. He has authored Pro Git and spoken globally on Git and software collaboration.

Website Twitter

Stay in the Loop

Subscribe to get fresh updates, insights, and exclusive content delivered straight to your inbox.
No spam, just great reads. 🚀