@@ -491,62 +491,61 @@ public static bool MergeUpstream( BuildContext context, UpstreamMergeSettings se
491491 }
492492 }
493493
494- // ==================== STEP 13: Create or Checkout Merge Branch ====================
494+ // ==================== STEP 13: Create Merge Branch (delete if exists) ====================
495495 context . Console . WriteMessage ( "" ) ;
496- context . Console . WriteMessage ( "Step 13: Creating or checking out merge branch..." ) ;
497-
498- bool targetBranchExists ;
496+ context . Console . WriteMessage ( "Step 13: Creating merge branch..." ) ;
499497
498+ // If the target branch exists (remotely or locally), delete it first.
499+ // This ensures we always start fresh from the current downstream branch,
500+ // avoiding stale state where the merge branch conflicts with an updated target.
500501 if ( targetBranchExistsRemotely )
501502 {
502- targetBranchExists = true ;
503- context . Console . WriteMessage ( "Branch exists on remote." ) ;
504- }
505- else
506- {
507- // Check if it exists locally
508- if ( ! GitHelper . TryGetCurrentCommitHash ( context , targetBranch , out var targetBranchCurrentCommitHash ) )
509- {
510- context . Console . WriteError ( "Failed to check if branch exists locally." ) ;
503+ context . Console . WriteImportantMessage ( $ "Merge branch '{ targetBranch } ' exists on remote. Deleting to start fresh..." ) ;
511504
512- return false ;
505+ if ( ! GitHelper . TryDeleteRemoteBranch ( context , targetBranch ) )
506+ {
507+ context . Console . WriteWarning ( $ "Failed to delete remote branch '{ targetBranch } '. Continuing anyway..." ) ;
513508 }
514-
515- targetBranchExists = targetBranchCurrentCommitHash != null ;
516-
517- if ( targetBranchExists )
509+ else
518510 {
519- context . Console . WriteMessage ( "Branch exists locally ." ) ;
511+ context . Console . WriteMessage ( "Remote branch deleted ." ) ;
520512 }
521513 }
522514
523- if ( targetBranchExists )
515+ // Check if it exists locally and delete
516+ if ( ! GitHelper . TryGetCurrentCommitHash ( context , targetBranch , out var targetBranchCurrentCommitHash ) )
524517 {
525- context . Console . WriteImportantMessage ( $ "Merge branch '{ targetBranch } ' already exists. Checking out and pulling latest..." ) ;
526-
527- if ( ! GitHelper . TryCheckoutAndPull ( context , targetBranch ) )
528- {
529- context . Console . WriteError ( "Failed to checkout and pull merge branch." ) ;
530-
531- return false ;
532- }
518+ context . Console . WriteError ( "Failed to check if branch exists locally." ) ;
533519
534- context . Console . WriteMessage ( "Successfully checked out merge branch." ) ;
520+ return false ;
535521 }
536- else
522+
523+ if ( targetBranchCurrentCommitHash != null )
537524 {
538- context . Console . WriteImportantMessage ( $ "Creating new merge branch: { targetBranch } " ) ;
525+ context . Console . WriteMessage ( $ "Merge branch ' { targetBranch } ' exists locally. Deleting... " ) ;
539526
540- if ( ! GitHelper . TryCreateBranch ( context , targetBranch ) )
527+ if ( ! GitHelper . TryDeleteLocalBranch ( context , targetBranch ) )
541528 {
542- context . Console . WriteError ( "Failed to create merge branch." ) ;
543-
544- return false ;
529+ context . Console . WriteWarning ( $ "Failed to delete local branch '{ targetBranch } '. Continuing anyway..." ) ;
530+ }
531+ else
532+ {
533+ context . Console . WriteMessage ( "Local branch deleted." ) ;
545534 }
535+ }
536+
537+ // Create fresh merge branch from current HEAD (the downstream branch)
538+ context . Console . WriteImportantMessage ( $ "Creating new merge branch: { targetBranch } " ) ;
539+
540+ if ( ! GitHelper . TryCreateBranch ( context , targetBranch ) )
541+ {
542+ context . Console . WriteError ( "Failed to create merge branch." ) ;
546543
547- context . Console . WriteMessage ( "Successfully created merge branch." ) ;
544+ return false ;
548545 }
549546
547+ context . Console . WriteMessage ( "Successfully created merge branch." ) ;
548+
550549 // ==================== STEP 14: Push Merge Branch ====================
551550 context . Console . WriteMessage ( "" ) ;
552551 context . Console . WriteMessage ( "Step 14: Pushing merge branch to remote..." ) ;
@@ -763,12 +762,6 @@ private static bool TryMerge(
763762
764763 context . Console . WriteMessage ( "" ) ;
765764 context . Console . WriteMessage ( "Invoking Claude Code to resolve conflicts..." ) ;
766- context . Console . WriteMessage ( "Claude will:" ) ;
767- context . Console . WriteMessage ( " 1. Read each conflicting file" ) ;
768- context . Console . WriteMessage ( " 2. Understand both sides of the conflict" ) ;
769- context . Console . WriteMessage ( " 3. Resolve the conflict intelligently" ) ;
770- context . Console . WriteMessage ( " 4. Run ./build.sh build to verify the resolution" ) ;
771- context . Console . WriteMessage ( "" ) ;
772765
773766 if ( ClaudeCodeHelper . TryResolveMergeConflicts (
774767 context . Console ,
0 commit comments