@@ -132,6 +132,7 @@ func createRun(opts *CreateOptions) error {
132132 isNameAnArg := false
133133 isDescEmpty := opts .Description == ""
134134 isVisibilityPassed := false
135+ inLocalRepo := projectDirErr == nil
135136
136137 if opts .Name != "" {
137138 isNameAnArg = true
@@ -201,6 +202,7 @@ func createRun(opts *CreateOptions) error {
201202 repoToCreate = ghrepo .New ("" , opts .Name )
202203 }
203204
205+ var templateRepoMainBranch string
204206 // Find template repo ID
205207 if opts .Template != "" {
206208 httpClient , err := opts .HttpClient ()
@@ -230,6 +232,7 @@ func createRun(opts *CreateOptions) error {
230232 }
231233
232234 opts .Template = repo .ID
235+ templateRepoMainBranch = repo .DefaultBranchRef .Name
233236 }
234237
235238 input := repoCreateInput {
@@ -250,7 +253,7 @@ func createRun(opts *CreateOptions) error {
250253
251254 createLocalDirectory := opts .ConfirmSubmit
252255 if ! opts .ConfirmSubmit {
253- opts .ConfirmSubmit , err = confirmSubmission (input .Name , input .OwnerID , projectDirErr )
256+ opts .ConfirmSubmit , err = confirmSubmission (input .Name , input .OwnerID , inLocalRepo )
254257 if err != nil {
255258 return err
256259 }
@@ -284,7 +287,7 @@ func createRun(opts *CreateOptions) error {
284287 }
285288 remoteURL := ghrepo .FormatRemoteURL (repo , protocol )
286289
287- if projectDirErr == nil {
290+ if inLocalRepo {
288291 _ , err = git .AddRemote ("origin" , remoteURL )
289292 if err != nil {
290293 return err
@@ -295,40 +298,26 @@ func createRun(opts *CreateOptions) error {
295298 } else {
296299 if opts .IO .CanPrompt () {
297300 if ! createLocalDirectory {
298- err := prompt .Confirm (fmt .Sprintf (" Create a local project directory for %s?" , ghrepo .FullName (repo )), & createLocalDirectory )
301+ err := prompt .Confirm (fmt .Sprintf (` Create a local project directory for "%s"?` , ghrepo .FullName (repo )), & createLocalDirectory )
299302 if err != nil {
300303 return err
301304 }
302305 }
303306 }
304307 if createLocalDirectory {
305308 path := repo .Name
306-
307- gitInit , err := git .GitCommand ("init" , path )
308- if err != nil {
309- return err
310- }
311- isTTY := opts .IO .IsStdoutTTY ()
312- if isTTY {
313- gitInit .Stdout = stdout
314- }
315- gitInit .Stderr = stderr
316- err = run .PrepareCmd (gitInit ).Run ()
317- if err != nil {
318- return err
319- }
320- gitRemoteAdd , err := git .GitCommand ("-C" , path , "remote" , "add" , "origin" , remoteURL )
321- if err != nil {
322- return err
309+ checkoutBranch := ""
310+ if opts .Template != "" {
311+ // NOTE: we cannot read `defaultBranchRef` from the newly created repository as it will
312+ // be null at this time. Instead, we assume that the main branch name of the new
313+ // repository will be the same as that of the template repository.
314+ checkoutBranch = templateRepoMainBranch
323315 }
324- gitRemoteAdd .Stdout = stdout
325- gitRemoteAdd .Stderr = stderr
326- err = run .PrepareCmd (gitRemoteAdd ).Run ()
327- if err != nil {
316+ if err := localInit (opts .IO , remoteURL , path , checkoutBranch ); err != nil {
328317 return err
329318 }
330319 if isTTY {
331- fmt .Fprintf (stderr , "%s Initialized repository in './%s/' \n " , cs .SuccessIcon (), path )
320+ fmt .Fprintf (stderr , "%s Initialized repository in \" %s \" \n " , cs .SuccessIcon (), path )
332321 }
333322 }
334323 }
@@ -339,6 +328,56 @@ func createRun(opts *CreateOptions) error {
339328 return nil
340329}
341330
331+ func localInit (io * iostreams.IOStreams , remoteURL , path , checkoutBranch string ) error {
332+ gitInit , err := git .GitCommand ("init" , path )
333+ if err != nil {
334+ return err
335+ }
336+ isTTY := io .IsStdoutTTY ()
337+ if isTTY {
338+ gitInit .Stdout = io .Out
339+ }
340+ gitInit .Stderr = io .ErrOut
341+ err = run .PrepareCmd (gitInit ).Run ()
342+ if err != nil {
343+ return err
344+ }
345+
346+ gitRemoteAdd , err := git .GitCommand ("-C" , path , "remote" , "add" , "origin" , remoteURL )
347+ if err != nil {
348+ return err
349+ }
350+ gitRemoteAdd .Stdout = io .Out
351+ gitRemoteAdd .Stderr = io .ErrOut
352+ err = run .PrepareCmd (gitRemoteAdd ).Run ()
353+ if err != nil {
354+ return err
355+ }
356+
357+ if checkoutBranch == "" {
358+ return nil
359+ }
360+
361+ gitFetch , err := git .GitCommand ("-C" , path , "fetch" , "origin" , fmt .Sprintf ("+refs/heads/%[1]s:refs/remotes/origin/%[1]s" , checkoutBranch ))
362+ if err != nil {
363+ return err
364+ }
365+ gitFetch .Stdout = io .Out
366+ gitFetch .Stderr = io .ErrOut
367+ err = run .PrepareCmd (gitFetch ).Run ()
368+ if err != nil {
369+ return err
370+ }
371+
372+ gitCheckout , err := git .GitCommand ("-C" , path , "checkout" , checkoutBranch )
373+ if err != nil {
374+ return err
375+ }
376+ gitCheckout .Stdout = io .Out
377+ gitCheckout .Stderr = io .ErrOut
378+ return run .PrepareCmd (gitCheckout ).Run ()
379+ }
380+
342381func interactiveRepoCreate (isDescEmpty bool , isVisibilityPassed bool , repoName string ) (string , string , string , error ) {
343382 qs := []* survey.Question {}
344383
@@ -388,16 +427,18 @@ func interactiveRepoCreate(isDescEmpty bool, isVisibilityPassed bool, repoName s
388427 return answers .RepoName , answers .RepoDescription , strings .ToUpper (answers .RepoVisibility ), nil
389428}
390429
391- func confirmSubmission (repoName string , repoOwner string , projectDirErr error ) (bool , error ) {
430+ func confirmSubmission (repoName string , repoOwner string , inLocalRepo bool ) (bool , error ) {
392431 qs := []* survey.Question {}
393432
394433 promptString := ""
395- if projectDirErr == nil {
396- promptString = "This will add remote origin to your current directory. Continue? "
397- } else if repoOwner != "" {
398- promptString = fmt .Sprintf ("This will create '%s/%s' in your current directory. Continue? " , repoOwner , repoName )
434+ if inLocalRepo {
435+ promptString = `This will add an "origin" git remote to your local repository. Continue?`
399436 } else {
400- promptString = fmt .Sprintf ("This will create '%s' in your current directory. Continue? " , repoName )
437+ targetRepo := repoName
438+ if repoOwner != "" {
439+ targetRepo = fmt .Sprintf ("%s/%s" , repoOwner , repoName )
440+ }
441+ promptString = fmt .Sprintf (`This will create the "%s" repository on GitHub. Continue?` , targetRepo )
401442 }
402443
403444 confirmSubmitQuestion := & survey.Question {
0 commit comments