fix(worktree): use hard reset to correct file tree when branch base is wrong (#1982)

* fix(worktree): use hard reset to correct file tree when branch base is wrong (#1981)

The worktree_branch_check mitigation detects when EnterWorktree creates
branches from main instead of the current feature branch, but used
git reset --soft to correct it. This only fixed the commit pointer —
the working tree still contained main's files, causing silent data loss
on merge-back when the agent's commits overwrote feature branch code.

Changed to git reset --hard which safely corrects both pointer and file
tree (the check runs before any agent work, so no changes to lose).
Also removed the broken rebase --onto attempt in execute-phase.md that
could replay main's commits onto the feature branch, and added post-reset
verification that aborts if the correction fails.

Updated documentation from "Windows" to "all platforms" since the
upstream EnterWorktree bug affects macOS, Linux, and Windows alike.

Closes #1981

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>

* fix(worktree): update settings.md worktree description to say cross-platform

Aligns with the workflow file updates — the EnterWorktree base-branch
bug affects all platforms, not just Windows.

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>

---------

Co-authored-by: Claude Opus 4.6 <noreply@anthropic.com>
This commit is contained in:
Tibsfox
2026-04-10 07:49:20 -07:00
committed by GitHub
parent 33575ba91d
commit adec4eef48
5 changed files with 17 additions and 12 deletions

View File

@@ -348,21 +348,24 @@ Execute each selected wave in sequence. Within a wave: parallel if `PARALLELIZAT
Run:
```bash
ACTUAL_BASE=$(git merge-base HEAD {EXPECTED_BASE})
CURRENT_HEAD=$(git rev-parse HEAD)
```
If `ACTUAL_BASE` != `{EXPECTED_BASE}` (i.e. the worktree branch was created from an older
base such as `main` instead of the feature branch HEAD), rebase onto the correct base:
base such as `main` instead of the feature branch HEAD), hard-reset to the correct base:
```bash
git rebase --onto {EXPECTED_BASE} $(git rev-parse --abbrev-ref HEAD~1 2>/dev/null || git rev-parse HEAD^) HEAD 2>/dev/null || true
# If rebase fails or is a no-op, reset the branch to start from the correct base:
git reset --soft {EXPECTED_BASE}
# Safe: this runs before any agent work, so no uncommitted changes to lose
git reset --hard {EXPECTED_BASE}
# Verify correction succeeded
if [ "$(git rev-parse HEAD)" != "{EXPECTED_BASE}" ]; then
echo "ERROR: Could not correct worktree base — aborting to prevent data loss"
exit 1
fi
```
If `ACTUAL_BASE` == `{EXPECTED_BASE}`: the branch base is correct, proceed immediately.
This check fixes a known issue on Windows where `EnterWorktree` creates branches from
`main` instead of the current feature branch HEAD.
This check fixes a known issue where `EnterWorktree` creates branches from
`main` instead of the current feature branch HEAD (affects all platforms).
</worktree_branch_check>
<parallel_execution>