Skip to content

Fix ResetFilesToTargetBranch to remove source-only files from merge#16466

Merged
akoeplinger merged 2 commits intomainfrom
copilot/fix-reset-files-merge-behavior
Jan 20, 2026
Merged

Fix ResetFilesToTargetBranch to remove source-only files from merge#16466
akoeplinger merged 2 commits intomainfrom
copilot/fix-reset-files-merge-behavior

Conversation

Copy link
Copy Markdown
Contributor

Copilot AI commented Jan 20, 2026

Problem

The ResetFilesToTargetBranch function fails to remove files that exist only in the source branch when using the ResetToTargetPaths parameter. git checkout silently fails for non-existent files in target, leaving source-only files in the merge PR.

Solution

Replace single-step checkout with two-step process:

  1. Remove all matching files from merge using git rm -rf <pattern>
  2. Restore from target using git checkout origin/$targetBranch -- <pattern>

This ensures correct behavior for all scenarios:

  • Files in both branches: Reset to target version
  • Files only in source: Deleted from merge
  • Files only in target: Restored (if deleted in source)

Changes

.github/workflows/scripts/inter-branch-merge.ps1 (lines 137-158):

# First, remove all files matching the pattern from the merge
$filesToRemove = & git ls-files $pattern 2>&1
if ($LASTEXITCODE -eq 0 -and $filesToRemove) {
    Write-Host -f Yellow "Removing files matching '$pattern' from merge"
    & git rm -rf $pattern 2>&1 | Write-Host
    $processedPatterns += $pattern
}

# Then, checkout any files that exist in target branch (re-adds them)
& git checkout "origin/$targetBranch" -- $pattern 2>&1 | Write-Host
if ($LASTEXITCODE -eq 0) {
    Write-Host -f Green "Checked out pattern '$pattern' from $targetBranch"
}

To double check:

Original prompt

Problem

In the ResetFilesToTargetBranch function in .github/workflows/scripts/inter-branch-merge.ps1, when using the ResetToTargetPaths parameter to reset files to the target branch version, files that exist in the source branch but not in the target branch are still included in the merge PR.

The current implementation only tries to checkout files from the target branch, but if files don't exist there, they remain unchanged in the merge branch.

Additionally, if a pattern matches files where some exist in the target branch and some don't, the ones that don't exist will still be incorrectly included.

Desired Behavior

Scenario Expected Behavior
File exists in both branches Reset to target branch version
File exists only in source branch Delete from merge
File exists only in target branch No change (will be added by merge)

Solution

Update the logic in the ResetFilesToTargetBranch function to:

  1. First, remove all files matching the pattern from the index using git rm
  2. Then, checkout files from the target branch (this re-adds only files that exist in target)

This ensures files that only exist in the source branch are removed from the merge, while files that exist in the target branch are reset to their target version.

Code Change

Replace the current pattern processing logic in the foreach ($pattern in $patterns) loop (around lines 139-151) with:

foreach ($pattern in $patterns) {
    $pattern = $pattern.Trim()
    if (-not $pattern) {
        continue
    }

    Write-Host "Processing pattern: $pattern"
    
    # First, remove all files matching the pattern from the merge
    $filesToRemove = & git ls-files $pattern 2>&1
    if ($LASTEXITCODE -eq 0 -and $filesToRemove) {
        Write-Host -f Yellow "Removing files matching '$pattern' from merge"
        & git rm -rf $pattern 2>&1 | Write-Host
        $processedPatterns += $pattern
    }
    
    # Then, checkout any files that exist in target branch (re-adds them)
    & git checkout "origin/$targetBranch" -- $pattern 2>&1 | Write-Host
    if ($LASTEXITCODE -eq 0) {
        Write-Host -f Green "Checked out pattern '$pattern' from $targetBranch"
    }
}

File to modify

.github/workflows/scripts/inter-branch-merge.ps1

This pull request was created from Copilot chat.


💬 We'd love your input! Share your thoughts on Copilot coding agent in our 2 minute survey.

…ranch

Co-authored-by: akoeplinger <1376924+akoeplinger@users.noreply.github.com>
Copilot AI changed the title [WIP] Fix file inclusion behavior in ResetFilesToTargetBranch Fix ResetFilesToTargetBranch to remove source-only files from merge Jan 20, 2026
Copilot AI requested a review from akoeplinger January 20, 2026 10:41
@akoeplinger akoeplinger merged commit 4d74430 into main Jan 20, 2026
2 of 4 checks passed
@akoeplinger akoeplinger deleted the copilot/fix-reset-files-merge-behavior branch January 20, 2026 15:49
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.

3 participants