Skip to content

Conversation

@lerenn
Copy link
Owner

@lerenn lerenn commented Jan 12, 2026

Problem

When creating a worktree for a branch that doesn't exist on the remote repository, the checkout process would fail with an error like:

fatal: 'origin/deploy-v0-0-67' is not a commit and a branch 'deploy-v0-0-67' cannot be created from it

This occurred because the code attempted to create a branch from origin/branch without first checking if that remote branch exists.

Solution

Modified CheckoutBranch in pkg/git/checkout_branch.go to:

  1. Check if the branch exists locally before fallback
  2. Check if origin/branch exists on remote before attempting to create from it
  3. Fetch from origin if remote branch doesn't exist, then check again
  4. Create branch from HEAD as final fallback if remote branch still doesn't exist

Testing

  • ✅ Added integration tests for various checkout scenarios
  • ✅ Added E2E tests to reproduce and verify fix for the original bug scenario
  • ✅ All tests pass (integration and E2E)

Changes

  • pkg/git/checkout_branch.go: Enhanced checkout logic with proper fallback handling
  • pkg/git/checkout_branch_test.go: Added integration tests
  • test/worktree_create_test.go: Added E2E tests for repo and workspace modes

Fixes the issue where worktree creation fails for branches that don't exist on the remote but need to be created locally.

…mote

Fix branch checkout failure when creating worktrees for branches that don't
exist on the remote repository. Previously, the code would attempt to create
a branch from origin/branch without checking if that remote branch exists,
resulting in errors like:
  fatal: 'origin/deploy-v0-0-67' is not a commit and a branch
  'deploy-v0-0-67' cannot be created from it

Changes:
- Modified CheckoutBranch to check if origin/branch exists before attempting
  to create from it
- Added fallback logic: fetch from origin, then check again
- Final fallback: create branch from HEAD if remote branch doesn't exist
- Added integration tests for various checkout scenarios
- Added E2E tests to reproduce and verify fix for the original bug scenario

Fixes the issue where worktree creation fails for branches like deploy-v0-0-67
that don't exist on the remote but need to be created locally.
Extract helper functions to reduce complexity from 14 to below 10:
- tryCheckoutBranch: attempts direct branch checkout
- tryCheckoutFromOrigin: attempts checkout from origin/branch
- createBranchFromHead: creates branch from HEAD

This addresses the golangci-lint cyclop linter requirement.
@lerenn lerenn merged commit 7901b6a into main Jan 12, 2026
9 checks passed
@lerenn lerenn deleted the fix-branch-checkout-bug branch January 12, 2026 16:28
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.

2 participants