Cursor's WorktreeManager force-deleted my git branch when cleaning up agent worktrees

Where does the bug appear (feature/product)?

Cursor IDE

Describe the Bug

(disclaimer: this is an AI-assisted bug report, I fully vetted and confirmed the details)

When using the Agent in worktree mode, Cursor incorrectly deleted my source branch (the branch I was on when starting the agent) during worktree cleanup. The cleanup logic runs git branch --contains <commit> and then force-deletes all matching branches, including my pre-existing working branch that was never created by Cursor.

My branch bug-reproduce-48193 was force-deleted with:

git branch -D bug-reproduce-48193 --force

This is data loss - my branch was destroyed without warning or confirmation.

Steps to Reproduce

  1. Create a new branch in your repository (e.g., bug-reproduce-48193)
  2. Start the Cursor Agent in worktree mode - Cursor creates worktrees in ~/.cursor/worktrees/<repo>/<id>/
  3. The agent works in detached HEAD worktrees based on your current branch’s commit
  4. Accept the agent’s changes and apply them to main branch
  5. Checkout to main branch in your main working directory
  6. Continue using Cursor normally
  7. Result: Your original branch (bug-reproduce-48193) is silently deleted

Expected Behavior

  • Cursor should only delete branches it explicitly created (e.g., beats-48193-jtr)
  • Cursor should never delete my pre-existing branches
  • At minimum, if branch deletion is intended, Cursor should prompt for confirmation

Operating System

Linux

Current Cursor Version (Menu → About Cursor → Copy)

Version: 2.2.36
VSCode Version: 1.105.1
Commit: 55c9bc11e99cedd1fb93fbb7996abf779c583150
Date: 2025-12-18T06:25:21.733Z
Electron: 37.5.1
Chromium: 138.0.7204.251
Node.js: 22.19.0
V8: 13.8.258.32-electron.0
OS: Linux x64 6.12.62-1-lts

Additional Information

Log Evidence

1. Initial state - I was on branch bug-reproduce-48193 at 10:08:52:

[Git.log] 2025-12-19 10:08:52.103 [info] > git config --get --local branch.bug-reproduce-48193.vscode-merge-base [5ms]
[Git.log] 2025-12-19 10:08:52.256 [info] > git config --add --local branch.bug-reproduce-48193.vscode-merge-base origin/main [3ms]

2. Worktree creation at 10:09:44 - Note “branch: undefined” indicates detached HEAD, based on my branch:

[Cursor Indexing & Retrieval.log] 2025-12-19 10:09:44.385 [info] Starting worktree creation with branch: undefined from base: bug-reproduce-48193 from cwd: /home/orestis/src/beats

[Git.log] 2025-12-19 10:09:44.500 [info] > git rev-parse bug-reproduce-48193 [7ms]
[Git.log] 2025-12-19 10:09:47.152 [info] > git worktree add -d /home/orestis/.cursor/worktrees/beats/zjm b95cc76490c9bb4184f98e0094be4af14b5d7bd2 [2639ms]
[Git.log] 2025-12-19 10:09:49.595 [info] > git worktree add --detach /home/orestis/.cursor/worktrees/beats/jtr b95cc76490c9bb4184f98e0094be4af14b5d7bd2 [2377ms]
[Git.log] 2025-12-19 10:09:52.139 [info] > git worktree add --detach /home/orestis/.cursor/worktrees/beats/lnb b95cc76490c9bb4184f98e0094be4af14b5d7bd2 [2363ms]

[Cursor Indexing & Retrieval.log] 2025-12-19 10:09:47.166 [info] Successfully created worktree with branch at /home/orestis/.cursor/worktrees/beats/zjm, branch: undefined, commit: b95cc76490c9bb4184f98e0094be4af14b5d7bd2

3. Worktree cleanup at 12:48:45 - THE BUG:

[renderer.log] 2025-12-19 12:48:45.093 [info] [Worktree] Removing worktree at /home/orestis/.cursor/worktrees/beats/jtr

[Git.log] 2025-12-19 12:48:45.105 [info] > git worktree list --porcelain [3ms]
[Git.log] 2025-12-19 12:48:45.129 [info] > git branch --contains b95cc76490c9bb4184f98e0094be4af14b5d7bd2 [15ms]
[Git.log] 2025-12-19 12:48:45.140 [info] > git branch -D bug-reproduce-48193 --force [2ms]   <-- MY BRANCH DELETED!
[Git.log] 2025-12-19 12:48:45.498 [info] > git worktree remove /home/orestis/.cursor/worktrees/beats/jtr --force [348ms]

[renderer.log] 2025-12-19 12:48:45.503 [info] [Worktree] Successfully removed worktree at /home/orestis/.cursor/worktrees/beats/jtr
[renderer.log] 2025-12-19 12:48:45.503 [info] [WorktreeManager] Removed worktree at /home/orestis/.cursor/worktrees/beats/jtr

Root Cause Analysis

The cleanup sequence shows:

  1. git worktree list --porcelain - list worktrees
  2. git branch --contains <commit> - find branches containing the worktree’s commit
  3. git branch -D <branch> --force - force-delete ALL branches found (BUG: this includes my original branch!)
  4. git worktree remove ... --force - remove the worktree

The bug is in step 3: Cursor should only delete branches it created (which would have names like beats-48193-jtr), not any branch that happens to contain the commit. My branch bug-reproduce-48193 existed before the agent started and was merely used as the base for the detached HEAD worktrees.

Suggested Fix

Track which branches Cursor creates (e.g., in a metadata file or by naming convention) and only delete those during cleanup. Never delete branches that existed before the worktree was created.

Does this stop you from using Cursor

No - Cursor works, but with this issue

1 Like

Hey, thanks for the report.

I see the cleanup logic uses git branch --contains and deletes all matching branches, not just the ones created by Cursor. That’s a clear bug, and I’m passing it to the team to fix.

As a temporary workaround: after the agent applies changes, you can manually remove worktrees with git worktree remove to avoid the automatic cleanup.

1 Like

This is very very bad. I lost alot due to this.

1 Like

@Vedant_Singh if you don’t run git gc it’s very likely all the git references are still there. Run git reflogand check all the references there.