Claude Code and Git Worktrees

The Claude Code docs suggest that if you want to run more that one Claude Code session simultaneously for the same project, you should use git worktrees. Today I actually tried to do that, and the experience was not great tbh.

A git worktree is just basically another copy of your repository, which gets checked out inside a directory of your choice. From the filesystem point of view, it looks like a complete separate clone of the same repository. In fact it's not a full clone - it shares the same history and the same working directory, so you on really large repos with lots of objects and history, worktrees should be faster and use less disk space than full clones. I don't think either of those two benefits actually ends up mattering that much, as we'll get back to in a moment.

Let's see how we would actually do that:

# this creates a new git worktree in the ./short-video directory, checking out
git worktree add short-video main

# cd into it
cd short-video

# make a new branch
git checkout -b short-video

# yeah you gotta do this each time
pnpm install

# and this kind of thing
cp ./.env.local ./short-video/.env.local

# then you can run claude code
claude
# this creates a new git worktree in the ./short-video directory, checking out
git worktree add short-video main

# cd into it
cd short-video

# make a new branch
git checkout -b short-video

# yeah you gotta do this each time
pnpm install

# and this kind of thing
cp ./.env.local ./short-video/.env.local

# then you can run claude code
claude

Ok that was a few steps, but not too bad. Now we have a subdirectory in our project called short-video that we can run Claude Code in once we've done all that.

The first time I actually got that working I was pretty happy, until Claude Code tried to run pnpm lint:fix for me:

Claude Code trying to run pnpm lint:fix results in an ES Lint conflict

Ooof. Well, this is nothing to do with Claude Code at all, but it doesn't stop it from sucking. There's a way around this, so long as our project doesn't actually rely on nested ES Lint configurations, which is to set root: true on your .eslintrc.json or equivalent file (you'll have to commit that change before you make your worktree, or else make the same change again inside the worktree directory).

Ok, but then we try to run pnpm test in our base directory and although things work, we get a bunch of warnings like this:

jest-haste-map: duplicate manual mock found: framer-motion
The following files share their name; please delete one of them:
* <rootDir>/test/__mocks__/framer-motion.tsx
* <rootDir>/.worktrees/short-video/test/__mocks__/framer-motion.tsx

jest-haste-map: duplicate manual mock found: ai/react
The following files share their name; please delete one of them:
* <rootDir>/test/__mocks__/ai/react.ts
* <rootDir>/.worktrees/short-video/test/__mocks__/ai/react.ts
jest-haste-map: duplicate manual mock found: framer-motion
The following files share their name; please delete one of them:
* <rootDir>/test/__mocks__/framer-motion.tsx
* <rootDir>/.worktrees/short-video/test/__mocks__/framer-motion.tsx

jest-haste-map: duplicate manual mock found: ai/react
The following files share their name; please delete one of them:
* <rootDir>/test/__mocks__/ai/react.ts
* <rootDir>/.worktrees/short-video/test/__mocks__/ai/react.ts

Ok so what's happening there is that my Jest mocks are being picked up twice when I run pnpm test in the base directory. This is because the worktree is a separate copy of the repository, and it has its own test directory with its own mocks. Jest doesn't know that it's a worktree and will try to pick up both sets of mocks. I'm sure there's a solution to that too, but this is starting to get a bit annoying.

What about a completely separate clone?

Cons:

  • Does not work well in IDEs - they can't see the history
  • You still have to pnpm install
  • ES Lint does not like it at all (collides with eslintrc in parent folder)

Issues that affect separate clones AND worktrees:

  • If you have migrated your database on one branch, the other will be out of sync (assuming you use the same local dev database)
  • If you are running a local server on one worktree/clone, changes that Claude made on the other won't show up

Share Post:

What to Read Next