@@ -14,7 +14,9 @@ defmodule GitWork.Commands.Checkout do
1414 Switch to a branch by navigating to its worktree directory.
1515
1616 Without -b, matches an existing worktree (exact or fuzzy) and prints its
17- path. Returns an error if no matching worktree is found.
17+ path. If no worktree matches but a remote branch with that exact name
18+ exists, a new worktree is automatically created tracking the remote branch.
19+ Returns an error only if neither a worktree nor a remote branch is found.
1820
1921 With -b, creates a new worktree for the given branch (tracking the remote
2022 branch if one exists, or creating a new local branch otherwise). Returns
@@ -30,6 +32,7 @@ defmodule GitWork.Commands.Checkout do
3032 Examples:
3133 git-work checkout feature-login # switch to existing worktree
3234 git-work checkout login # fuzzy match existing worktree
35+ git-work checkout feature/remote # auto-create from remote branch
3336 git-work checkout -b feature/new # create new worktree
3437 """
3538 end
@@ -68,7 +71,20 @@ defmodule GitWork.Commands.Checkout do
6871 { :error , "ambiguous match for '#{ input } ':\n #{ formatted } " }
6972
7073 :no_match ->
71- { :error , "no worktree found for '#{ input } ' (use -b to create one)" }
74+ # No existing worktree — check if a remote branch matches
75+ bare_dir = Project . bare_path ( root )
76+
77+ case Git . cmd ( [ "branch" , "-r" , "--list" , "origin/#{ input } " ] , cd: bare_dir ) do
78+ { :ok , "" } ->
79+ { :error , "no worktree found for '#{ input } ' (use -b to create one)" }
80+
81+ { :ok , _ } ->
82+ IO . write ( :stderr , "creating worktree from remote branch: '#{ input } '\n " )
83+ create_worktree ( root , input , sanitized )
84+
85+ { :error , _ } ->
86+ { :error , "no worktree found for '#{ input } ' (use -b to create one)" }
87+ end
7288 end
7389 end
7490
0 commit comments