Skip to content

fix(worktree): detect default branch dynamically#28

Merged
mathix420 merged 1 commit into
masterfrom
fix-main-branch-different-than-master
May 11, 2026
Merged

fix(worktree): detect default branch dynamically#28
mathix420 merged 1 commit into
masterfrom
fix-main-branch-different-than-master

Conversation

@mathix420
Copy link
Copy Markdown
Owner

Summary

  • Worktree creation no longer fails with `base branch `master` not found locally or on origin` when the user has a global `init.defaultBranch=master` but the repo's actual default is `main`/`alacritree`/etc.
  • `detect_default_branch` now consults `refs/remotes/origin/HEAD` first, then common local names, and only honors `init.defaultBranch` when it points to a real local branch.
  • At create time, `worktree::resolve_base_branch` adds a runtime fallback that asks origin via `git ls-remote --symref origin HEAD`, so a stale or missing local hint can't block creation.

Test plan

  • On a repo with global `init.defaultBranch=master` but `origin/HEAD -> origin/alacritree`, create a new worktree — base branch resolves to `alacritree`.
  • On a repo where `refs/remotes/origin/HEAD` is unset (clone --depth=1), worktree create still succeeds via the `ls-remote --symref` fallback.
  • Existing happy path on a `main`-defaulted repo is unchanged.

🤖 Generated with Claude Code

`detect_default_branch` checked `init.defaultBranch` first, but that
config governs what `git init` names new repos — not the default branch
of the current one. A global `init.defaultBranch=master` therefore
hijacked repos whose actual default is `main`/`alacritree`/etc., and
worktree creation failed with "base branch `master` not found locally
or on origin".

Now the cascade is: `refs/remotes/origin/HEAD` → common local names
(main/master/trunk/develop) → `init.defaultBranch` only if it names a
real local branch. Worktree creation adds a runtime fallback that
queries origin via `git ls-remote --symref origin HEAD` when the cached
hint doesn't resolve, so a stale or missing local hint no longer blocks
creation.

Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
@mathix420 mathix420 merged commit 5191fef into master May 11, 2026
2 checks passed
@mathix420 mathix420 deleted the fix-main-branch-different-than-master branch May 11, 2026 16:40
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.

1 participant