@@ -33,6 +33,7 @@ type CheckoutOptions struct {
3333 RecurseSubmodules bool
3434 Force bool
3535 Detach bool
36+ BranchName string
3637}
3738
3839func NewCmdCheckout (f * cmdutil.Factory , runF func (* CheckoutOptions ) error ) * cobra.Command {
@@ -65,6 +66,7 @@ func NewCmdCheckout(f *cmdutil.Factory, runF func(*CheckoutOptions) error) *cobr
6566 cmd .Flags ().BoolVarP (& opts .RecurseSubmodules , "recurse-submodules" , "" , false , "Update all submodules after checkout" )
6667 cmd .Flags ().BoolVarP (& opts .Force , "force" , "f" , false , "Reset the existing local branch to the latest state of the pull request" )
6768 cmd .Flags ().BoolVarP (& opts .Detach , "detach" , "" , false , "Checkout PR with a detached HEAD" )
69+ cmd .Flags ().StringVarP (& opts .BranchName , "branch" , "b" , "" , "Local branch name to use (default: the name of the head branch)" )
6870
6971 return cmd
7072}
@@ -139,7 +141,6 @@ func checkoutRun(opts *CheckoutOptions) error {
139141
140142func cmdsForExistingRemote (remote * context.Remote , pr * api.PullRequest , opts * CheckoutOptions ) [][]string {
141143 var cmds [][]string
142-
143144 remoteBranch := fmt .Sprintf ("%s/%s" , remote .Name , pr .HeadRefName )
144145
145146 refSpec := fmt .Sprintf ("+refs/heads/%s" , pr .HeadRefName )
@@ -149,21 +150,29 @@ func cmdsForExistingRemote(remote *context.Remote, pr *api.PullRequest, opts *Ch
149150
150151 cmds = append (cmds , []string {"git" , "fetch" , remote .Name , refSpec })
151152
153+ localBranch := pr .HeadRefName
154+ if opts .BranchName != "" {
155+ localBranch = opts .BranchName
156+ }
157+
152158 switch {
153159 case opts .Detach :
154160 cmds = append (cmds , []string {"git" , "checkout" , "--detach" , "FETCH_HEAD" })
155- case localBranchExists (pr . HeadRefName ):
156- cmds = append (cmds , []string {"git" , "checkout" , pr . HeadRefName })
161+ case localBranchExists (localBranch ):
162+ cmds = append (cmds , []string {"git" , "checkout" , localBranch })
157163 if opts .Force {
158164 cmds = append (cmds , []string {"git" , "reset" , "--hard" , fmt .Sprintf ("refs/remotes/%s" , remoteBranch )})
159165 } else {
160166 // TODO: check if non-fast-forward and suggest to use `--force`
161167 cmds = append (cmds , []string {"git" , "merge" , "--ff-only" , fmt .Sprintf ("refs/remotes/%s" , remoteBranch )})
162168 }
163169 default :
164- cmds = append (cmds , []string {"git" , "checkout" , "-b" , pr .HeadRefName , "--no-track" , remoteBranch })
165- cmds = append (cmds , []string {"git" , "config" , fmt .Sprintf ("branch.%s.remote" , pr .HeadRefName ), remote .Name })
166- cmds = append (cmds , []string {"git" , "config" , fmt .Sprintf ("branch.%s.merge" , pr .HeadRefName ), "refs/heads/" + pr .HeadRefName })
170+ cmds = append (
171+ cmds ,
172+ []string {"git" , "checkout" , "-b" , localBranch , "--no-track" , remoteBranch },
173+ []string {"git" , "config" , fmt .Sprintf ("branch.%s.remote" , localBranch ), remote .Name },
174+ []string {"git" , "config" , fmt .Sprintf ("branch.%s.merge" , pr .HeadRefName ), "refs/heads/" + pr .HeadRefName },
175+ )
167176 }
168177
169178 return cmds
@@ -204,7 +213,12 @@ func cmdsForMissingRemote(pr *api.PullRequest, baseURLOrName, repoHost, defaultB
204213 // TODO: check if non-fast-forward and suggest to use `--force`
205214 cmds = append (cmds , []string {"git" , "fetch" , baseURLOrName , fmt .Sprintf ("%s:%s" , ref , newBranchName )})
206215 }
207- cmds = append (cmds , []string {"git" , "checkout" , newBranchName })
216+
217+ if opts .BranchName != "" {
218+ cmds = append (cmds , []string {"git" , "checkout" , "-b" , opts .BranchName , "--track" , newBranchName })
219+ } else {
220+ cmds = append (cmds , []string {"git" , "checkout" , newBranchName })
221+ }
208222 }
209223
210224 remote := baseURLOrName
0 commit comments