@@ -27,6 +27,7 @@ type CloneOptions struct {
2727 GitArgs []string
2828 Repository string
2929 UpstreamName string
30+ NoUpstream bool
3031}
3132
3233func NewCmdClone (f * cmdutil.Factory , runF func (* CloneOptions ) error ) * cobra.Command {
@@ -60,6 +61,7 @@ func NewCmdClone(f *cmdutil.Factory, runF func(*CloneOptions) error) *cobra.Comm
6061 the remote after the owner of the parent repository.
6162
6263 If the repository is a fork, its parent repository will be set as the default remote repository.
64+ To skip this behavior, use %[1]s--no-upstream%[1]s.
6365 ` , "`" ),
6466 Example : heredoc .Doc (`
6567 # Clone a repository from a specific org
@@ -77,6 +79,9 @@ func NewCmdClone(f *cmdutil.Factory, runF func(*CloneOptions) error) *cobra.Comm
7779
7880 # Clone a repository with additional git clone flags
7981 $ gh repo clone cli/cli -- --depth=1
82+
83+ # Clone a fork without adding an upstream remote
84+ $ gh repo clone myfork --no-upstream
8085 ` ),
8186 RunE : func (cmd * cobra.Command , args []string ) error {
8287 opts .Repository = args [0 ]
@@ -91,6 +96,8 @@ func NewCmdClone(f *cmdutil.Factory, runF func(*CloneOptions) error) *cobra.Comm
9196 }
9297
9398 cmd .Flags ().StringVarP (& opts .UpstreamName , "upstream-remote-name" , "u" , "upstream" , "Upstream remote name when cloning a fork" )
99+ cmd .Flags ().BoolVar (& opts .NoUpstream , "no-upstream" , false , "Do not add an upstream remote when cloning a fork" )
100+ cmd .MarkFlagsMutuallyExclusive ("upstream-remote-name" , "no-upstream" )
94101 cmd .SetFlagErrorFunc (func (cmd * cobra.Command , err error ) error {
95102 if err == pflag .ErrHelp {
96103 return err
@@ -187,37 +194,43 @@ func cloneRun(opts *CloneOptions) error {
187194
188195 // If the repo is a fork, add the parent as an upstream remote and set the parent as the default repo.
189196 if canonicalRepo .Parent != nil {
190- protocol := cfg .GitProtocol (canonicalRepo .Parent .RepoHost ()).Value
191- upstreamURL := ghrepo .FormatRemoteURL (canonicalRepo .Parent , protocol )
192-
193- upstreamName := opts .UpstreamName
194- if opts .UpstreamName == "@owner" {
195- upstreamName = canonicalRepo .Parent .RepoOwner ()
196- }
197-
198197 gc := gitClient .Copy ()
199198 gc .RepoDir = cloneDir
200199
201- if _ , err := gc .AddRemote (ctx , upstreamName , upstreamURL , []string {canonicalRepo .Parent .DefaultBranchRef .Name }); err != nil {
202- return err
203- }
200+ if opts .NoUpstream {
201+ if err := gc .SetRemoteResolution (ctx , "origin" , "base" ); err != nil {
202+ return err
203+ }
204+ } else {
205+ protocol := cfg .GitProtocol (canonicalRepo .Parent .RepoHost ()).Value
206+ upstreamURL := ghrepo .FormatRemoteURL (canonicalRepo .Parent , protocol )
204207
205- if err := gc .Fetch (ctx , upstreamName , "" ); err != nil {
206- return err
207- }
208+ upstreamName := opts .UpstreamName
209+ if opts .UpstreamName == "@owner" {
210+ upstreamName = canonicalRepo .Parent .RepoOwner ()
211+ }
208212
209- if err := gc .SetRemoteBranches (ctx , upstreamName , `*` ); err != nil {
210- return err
211- }
213+ if _ , err := gc .AddRemote (ctx , upstreamName , upstreamURL , [] string { canonicalRepo . Parent . DefaultBranchRef . Name } ); err != nil {
214+ return err
215+ }
212216
213- if err = gc .SetRemoteResolution (ctx , upstreamName , "base " ); err != nil {
214- return err
215- }
217+ if err : = gc .Fetch (ctx , upstreamName , "" ); err != nil {
218+ return err
219+ }
216220
217- connectedToTerminal := opts .IO .IsStdoutTTY ()
218- if connectedToTerminal {
219- cs := opts .IO .ColorScheme ()
220- fmt .Fprintf (opts .IO .ErrOut , "%s Repository %s set as the default repository. To learn more about the default repository, run: gh repo set-default --help\n " , cs .WarningIcon (), cs .Bold (ghrepo .FullName (canonicalRepo .Parent )))
221+ if err := gc .SetRemoteBranches (ctx , upstreamName , `*` ); err != nil {
222+ return err
223+ }
224+
225+ if err := gc .SetRemoteResolution (ctx , upstreamName , "base" ); err != nil {
226+ return err
227+ }
228+
229+ connectedToTerminal := opts .IO .IsStdoutTTY ()
230+ if connectedToTerminal {
231+ cs := opts .IO .ColorScheme ()
232+ fmt .Fprintf (opts .IO .ErrOut , "%s Repository %s set as the default repository. To learn more about the default repository, run: gh repo set-default --help\n " , cs .WarningIcon (), cs .Bold (ghrepo .FullName (canonicalRepo .Parent )))
233+ }
221234 }
222235 }
223236 return nil
0 commit comments