git: worktree, fix successive branch checkouts from same commit. Fixes #2124#2127
Open
AriehSchneier wants to merge 1 commit into
Open
git: worktree, fix successive branch checkouts from same commit. Fixes #2124#2127AriehSchneier wants to merge 1 commit into
AriehSchneier wants to merge 1 commit into
Conversation
…go-git#2124 When checking out multiple branches from the same base commit with Force:true, files from previous iterations were incorrectly preserved in the worktree. Root cause: Checkout() updated HEAD before calling Reset(), so when Reset() captured the "previous" tree via headTree(), it got the tree from the NEW commit (just set by Checkout) rather than the OLD commit (where the worktree actually was). This caused resetWorktreeToTree to diff the same tree against itself, resulting in no changes. Fix: Capture the current tree BEFORE updating HEAD in Checkout(), and pass it to Reset() via a new internal field in ResetOptions. This ensures HardReset and KeepReset properly diff from the actual previous state. Assisted-by: Claude Sonnet 4.5 <[email protected]> Signed-off-by: Arieh Schneier <[email protected]>
3304cfe to
1515c02
Compare
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
Fixes #2124 - When checking out multiple branches from the same base commit with
Force:true, files from previous iterations were incorrectly preserved in the worktree.Root Cause
Checkout()updated HEAD before callingReset(), so whenReset()captured the "previous" tree viaheadTree(), it got the tree from the NEW commit (just set by Checkout) rather than the OLD commit (where the worktree actually was). This causedresetWorktreeToTreeto diff the same tree against itself, resulting in no changes.Solution
Capture the current tree BEFORE updating HEAD in
Checkout(), and pass it toReset()via a new internal field inResetOptions. This ensuresHardResetandKeepResetproperly diff from the actual previous state.Changes
fromTreefield toResetOptionsCheckout()to capture tree before updating HEADReset()to use providedfromTreewhen availableTestCheckoutSuccessiveBranchesFromSameCommitTesting
AI Disclosure
This contribution was assisted by Claude Sonnet 4.5 as documented in the
Assisted-by:commit trailer, in accordance with the AI Contribution Policy.