@@ -115,10 +115,17 @@ resolve_base_dir() {
115115 printf " %s" " $base_dir "
116116}
117117
118+ # Resolve the default remote name
119+ # Usage: resolve_default_remote
120+ resolve_default_remote () {
121+ cfg_default " gtr.defaultRemote" " GTR_DEFAULT_REMOTE" " origin"
122+ }
123+
118124# Resolve the default branch name
119- # Usage: resolve_default_branch [repo_root]
125+ # Usage: resolve_default_branch [repo_root] [remote]
120126resolve_default_branch () {
121127 local repo_root=" ${1:- $(pwd)} "
128+ local remote=" ${2:- $(resolve_default_remote)} "
122129 local default_branch
123130 local configured_branch
124131
@@ -130,18 +137,19 @@ resolve_default_branch() {
130137 return 0
131138 fi
132139
133- # Auto-detect from origin/HEAD
134- default_branch=$( git symbolic-ref --quiet refs/remotes/origin/HEAD 2> /dev/null | sed ' s|refs/remotes/origin/||' )
140+ # Auto-detect from the selected remote's HEAD
141+ default_branch=$( git symbolic-ref --quiet " refs/remotes/$remote /HEAD" 2> /dev/null || true)
142+ default_branch=" ${default_branch# refs/ remotes/ " $remote " / } "
135143
136144 if [ -n " $default_branch " ]; then
137145 printf " %s" " $default_branch "
138146 return 0
139147 fi
140148
141149 # Fallback: try common branch names
142- if git show-ref --verify --quiet " refs/remotes/origin /main" ; then
150+ if git show-ref --verify --quiet " refs/remotes/$remote /main" ; then
143151 printf " main"
144- elif git show-ref --verify --quiet " refs/remotes/origin /master" ; then
152+ elif git show-ref --verify --quiet " refs/remotes/$remote /master" ; then
145153 printf " master"
146154 else
147155 # Last resort: just use 'main'
@@ -370,34 +378,36 @@ _resolve_folder_name() {
370378
371379# Check if a branch exists on remote and/or locally.
372380# Sets globals: _wt_remote_exists, _wt_local_exists (0 or 1)
373- # Usage: _check_branch_refs <branch_name>
381+ # Usage: _check_branch_refs <branch_name> [remote]
374382declare _wt_remote_exists _wt_local_exists
375383_check_branch_refs () {
384+ local branch_name=" $1 " remote=" ${2:- $(resolve_default_remote)} "
385+
376386 _wt_remote_exists=0
377387 _wt_local_exists=0
378- git show-ref --verify --quiet " refs/remotes/origin/ $1 " && _wt_remote_exists=1
379- git show-ref --verify --quiet " refs/heads/$1 " && _wt_local_exists=1
388+ git show-ref --verify --quiet " refs/remotes/$remote / $branch_name " && _wt_remote_exists=1
389+ git show-ref --verify --quiet " refs/heads/$branch_name " && _wt_local_exists=1
380390 return 0
381391}
382392
383393# Auto-track: create local tracking branch from remote if needed, then add worktree.
384- # Usage: _worktree_add_tracked <worktree_path> <branch_name> [force_args...]
394+ # Usage: _worktree_add_tracked <worktree_path> <branch_name> [remote] [ force_args...]
385395# shellcheck disable=SC2317 # Called indirectly from create_worktree
386396_worktree_add_tracked () {
387- local wt_path=" $1 " branch_name=" $2 "
388- shift 2
397+ local wt_path=" $1 " branch_name=" $2 " remote= " ${3 :- $(resolve_default_remote)} "
398+ shift 3
389399
390- log_step " Branch '$branch_name ' exists on remote"
391- if git branch --track " $branch_name " " origin /$branch_name " > /dev/null 2>&1 ; then
392- log_info " Created local branch tracking origin /$branch_name "
400+ log_step " Branch '$branch_name ' exists on $ remote"
401+ if git branch --track " $branch_name " " $remote /$branch_name " > /dev/null 2>&1 ; then
402+ log_info " Created local branch tracking $remote /$branch_name "
393403 fi
394404 _try_worktree_add " $wt_path " " " \
395- " Worktree created tracking origin /$branch_name " \
405+ " Worktree created tracking $remote /$branch_name " \
396406 " $@ " " $branch_name "
397407}
398408
399409# Create a new git worktree
400- # Usage: create_worktree base_dir prefix branch_name from_ref track_mode [skip_fetch] [force] [custom_name] [folder_override]
410+ # Usage: create_worktree base_dir prefix branch_name from_ref track_mode [skip_fetch] [force] [custom_name] [folder_override] [remote]
401411# track_mode: auto, remote, local, or none
402412# skip_fetch: 0 (default, fetch) or 1 (skip)
403413# force: 0 (default, check branch) or 1 (allow same branch in multiple worktrees)
@@ -407,6 +417,7 @@ create_worktree() {
407417 local base_dir=" $1 " prefix=" $2 " branch_name=" $3 " from_ref=" $4 "
408418 local track_mode=" ${5:- auto} " skip_fetch=" ${6:- 0} " force=" ${7:- 0} "
409419 local custom_name=" ${8:- } " folder_override=" ${9:- } "
420+ local remote=" ${10:- $(resolve_default_remote)} "
410421
411422 local sanitized_name
412423 sanitized_name=$( _resolve_folder_name " $branch_name " " $custom_name " " $folder_override " ) || return 1
@@ -424,31 +435,31 @@ create_worktree() {
424435
425436 if [ " $skip_fetch " -eq 0 ]; then
426437 log_step " Fetching remote branches..."
427- git fetch origin 2> /dev/null || log_warn " Could not fetch from origin "
438+ git fetch " $remote " 2> /dev/null || log_warn " Could not fetch from $remote "
428439 fi
429440
430- _check_branch_refs " $branch_name "
441+ _check_branch_refs " $branch_name " " $remote "
431442
432443 # Resolve from_ref to a commit SHA to prevent git's guess-remote logic
433444 # from overriding the -b flag when from_ref matches a remote branch name.
434- # Try the ref as-is first, then with origin/ prefix for remote-only refs.
445+ # Try the ref as-is first, then with the selected remote prefix for remote-only refs.
435446 local resolved_ref
436447 resolved_ref=$( git rev-parse --verify " ${from_ref} ^{commit}" 2> /dev/null) \
437- || resolved_ref=$( git rev-parse --verify " origin /${from_ref} ^{commit}" 2> /dev/null) \
448+ || resolved_ref=$( git rev-parse --verify " $remote /${from_ref} ^{commit}" 2> /dev/null) \
438449 || resolved_ref=" $from_ref "
439450
440451 case " $track_mode " in
441452 remote)
442453 if [ " $_wt_remote_exists " -eq 1 ]; then
443454 _try_worktree_add " $worktree_path " \
444- " Creating worktree from remote branch origin /$branch_name " \
445- " Worktree created tracking origin /$branch_name " \
446- " ${force_args[@]} " -b " $branch_name " " origin /$branch_name " && return 0
455+ " Creating worktree from remote branch $remote /$branch_name " \
456+ " Worktree created tracking $remote /$branch_name " \
457+ " ${force_args[@]} " -b " $branch_name " " $remote /$branch_name " && return 0
447458 _try_worktree_add " $worktree_path " " " \
448- " Worktree created tracking origin /$branch_name " \
459+ " Worktree created tracking $remote /$branch_name " \
449460 " ${force_args[@]} " " $branch_name " && return 0
450461 fi
451- log_error " Remote branch origin /$branch_name does not exist"
462+ log_error " Remote branch $remote /$branch_name does not exist"
452463 return 1
453464 ;;
454465
@@ -474,7 +485,7 @@ create_worktree() {
474485
475486 auto|* )
476487 if [ " $_wt_remote_exists " -eq 1 ] && [ " $_wt_local_exists " -eq 0 ]; then
477- _worktree_add_tracked " $worktree_path " " $branch_name " " ${force_args[@]} " && return 0
488+ _worktree_add_tracked " $worktree_path " " $branch_name " " $remote " " $ {force_args[@]}" && return 0
478489 elif [ " $_wt_local_exists " -eq 1 ]; then
479490 _try_worktree_add " $worktree_path " \
480491 " Using existing local branch $branch_name " \
0 commit comments