Skip to content

fix: add --force flag to worktree creation for existing branches#5931

Closed
Githubguy132010 wants to merge 3 commits intoKilo-Org:mainfrom
Githubguy132010:fix/worktree-force-flag
Closed

fix: add --force flag to worktree creation for existing branches#5931
Githubguy132010 wants to merge 3 commits intoKilo-Org:mainfrom
Githubguy132010:fix/worktree-force-flag

Conversation

@Githubguy132010
Copy link
Contributor

@Githubguy132010 Githubguy132010 commented Feb 17, 2026

Context

When creating a worktree for an existing branch (like main), Git refuses if that branch is already checked out in another worktree. This prevents users from running multiple agents on the same branch (e.g., main) with their own worktrees.

The error was:

fatal: 'main' is already checked out at '/workspaces/kilocode'

Implementation

Added the --force flag to the git worktree add command when using an existing branch in WorktreeManager.createWorktree(). This allows the same branch to be checked out in multiple worktrees simultaneously.

Changes:

  • src/core/kilocode/agent-manager/WorktreeManager.ts - Added --force flag to worktree creation command
  • src/core/kilocode/agent-manager/__tests__/WorktreeManager.test.ts - Updated test to expect --force flag

Screenshots

N/A

How to Test

  1. Open a project on the main branch
  2. Open the Agent Manager
  3. Create a new agent session with "Worktree" mode selected
  4. Select main as the branch (or leave unselected to use current branch)
  5. The worktree should be created successfully without the "already checked out" error

Get in Touch

thomas07374

When creating a worktree for an existing branch (like main), Git refuses
if that branch is already checked out in another worktree. This fix adds
the --force flag to allow multiple agents to run on main with their own
worktrees.

Fixes: 'main' is already checked out error when creating worktrees
@chatgpt-codex-connector
Copy link

You have reached your Codex usage limits for code reviews. You can see your limits in the Codex usage dashboard.

@changeset-bot
Copy link

changeset-bot bot commented Feb 17, 2026

🦋 Changeset detected

Latest commit: 24f97b3

The changes in this PR will be included in the next version bump.

This PR includes changesets to release 1 package
Name Type
kilo-code Patch

Not sure what this means? Click here to learn what changesets are.

Click here if you're a maintainer who wants to add another changeset to this PR

@marius-kilocode
Copy link
Collaborator

A better approach that we can adapt: We allow running multiple agents in the same worktree. Thanks for the input!

@Githubguy132010
Copy link
Contributor Author

@marius-kilocode wouldn't the agents conflict with eachother?

@marius-kilocode
Copy link
Collaborator

Using --force with worktrees is pretty dangerous. As a commit in each of them will get the out of sync. I recommend you a different workflow. The new agent manager will handle multiple agents in multiple worktrees. I am sure this will solve your problem.

@Githubguy132010 Githubguy132010 deleted the fix/worktree-force-flag branch February 22, 2026 08:36
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants