@@ -97,8 +97,8 @@ func (c *Creator) CreatePullRequest(ctx context.Context, cfg config.RepositoryCo
9797 return fmt .Errorf ("GitHub API error: %w" , err )
9898 }
9999
100- // 5. Return to base branch for next iteration (handles CI environment)
101- if _ , err := RunGitCommand (ctx , c .workDir , "checkout" , "-B" , c . baseBranch , "origin/" + c .baseBranch ); err != nil {
100+ // 5. Return to base branch for next iteration
101+ if _ , err := RunGitCommand (ctx , c .workDir , "checkout" , c .baseBranch ); err != nil {
102102 fmt .Printf (" ⚠️ Warning: failed to checkout %s: %v\n " , c .baseBranch , err )
103103 }
104104
@@ -113,10 +113,33 @@ func (c *Creator) createBranch(ctx context.Context, branchName string) error {
113113 }
114114 }
115115
116- // Checkout base branch - create/reset from origin if needed (handles CI environment)
117- // Using -B ensures the branch is created from origin/base if it doesn't exist locally
118- if _ , err := RunGitCommand (ctx , c .workDir , "checkout" , "-B" , c .baseBranch , "origin/" + c .baseBranch ); err != nil {
119- return err
116+ // Try to fetch the base branch from origin
117+ // If this fails, we'll try to use the local branch if it exists
118+ fetchSucceeded := false
119+ if _ , err := RunGitCommand (ctx , c .workDir , "fetch" , "origin" , c .baseBranch ); err == nil {
120+ fetchSucceeded = true
121+ }
122+
123+ // Check if base branch exists locally
124+ baseExistsLocally := c .branchExists (ctx , c .baseBranch )
125+
126+ if ! baseExistsLocally && ! fetchSucceeded {
127+ // Can't proceed without either local branch or successful fetch
128+ return fmt .Errorf ("base branch %s does not exist locally and fetch from origin failed" , c .baseBranch )
129+ }
130+
131+ if fetchSucceeded {
132+ // Create/reset local branch to match FETCH_HEAD (latest from remote)
133+ // -B creates the branch if it doesn't exist, or resets it if it does
134+ if _ , err := RunGitCommand (ctx , c .workDir , "checkout" , "-B" , c .baseBranch , "FETCH_HEAD" ); err != nil {
135+ return fmt .Errorf ("failed to setup base branch %s from remote: %w" , c .baseBranch , err )
136+ }
137+ } else {
138+ // Fetch failed but local branch exists - use local copy
139+ if _ , err := RunGitCommand (ctx , c .workDir , "checkout" , c .baseBranch ); err != nil {
140+ return fmt .Errorf ("failed to checkout base branch %s: %w" , c .baseBranch , err )
141+ }
142+ fmt .Printf (" ⚠️ Warning: using local %s branch (fetch failed)\n " , c .baseBranch )
120143 }
121144
122145 // Create and checkout new branch
0 commit comments