Add --no-upstream flag to repo clone#12686
Conversation
When cloning a forked repository, `gh repo clone` automatically adds the parent repo as an `upstream` remote and sets it as the default repository. This can be problematic when the user lacks access to the parent repo, the upstream fetch is expensive for large repos, or the user simply doesn't want the upstream remote. Add a `--no-upstream` flag that skips adding the upstream remote when cloning a fork. When used, origin (the fork) is set as the default repository instead. The flag is mutually exclusive with `--upstream-remote-name`. For non-fork repos the flag is a harmless no-op. Closes cli#8274
There was a problem hiding this comment.
Pull request overview
This PR adds a --no-upstream flag to gh repo clone to address issue #8274. When cloning a forked repository, the current behavior automatically adds the parent repository as an upstream remote, which can cause permission errors or expensive fetches when the user lacks access to or doesn't need the parent repo. The new flag allows users to skip adding the upstream remote entirely, with the fork itself becoming the default repository.
Changes:
- Added
--no-upstreamboolean flag that is mutually exclusive with--upstream-remote-name - Modified fork handling logic to conditionally set
originas the base remote when--no-upstreamis true - Added comprehensive tests for flag parsing, mutual exclusion, and integration scenarios
Reviewed changes
Copilot reviewed 2 out of 2 changed files in this pull request and generated no comments.
| File | Description |
|---|---|
| pkg/cmd/repo/clone/clone.go | Adds NoUpstream field to CloneOptions, registers the flag with mutual exclusivity constraint, updates fork handling to conditionally skip upstream remote setup, and adds documentation |
| pkg/cmd/repo/clone/clone_test.go | Adds unit tests for flag parsing, mutual exclusion validation, and integration tests for both fork and non-fork scenarios with --no-upstream |
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
BagToad
left a comment
There was a problem hiding this comment.
Reviewed and tested locally — correctly sets origin as base resolution when --no-upstream is used, enforces mutual exclusion with --upstream-remote-name, and is a harmless no-op for non-forks. I pushed a small doc tweak to combine the help text lines. LGTM.
Summary
Closes #8274
When cloning a forked repository,
gh repo cloneautomatically adds the parent repo as anupstreamremote and sets it as the default repository. This can be problematic when:This PR adds a
--no-upstreamflag that skips adding the upstream remote when cloning a fork.Behavior
SetRemoteResolution--no-upstream+ forkoriginset asbase(the fork becomes the default repo)upstreamset asbase(existing behavior, unchanged)SetRemoteResolutioncall (existing behavior, unchanged)--no-upstreamis mutually exclusive with--upstream-remote-name(enforced viacobra.MarkFlagsMutuallyExclusive)--no-upstreamis a harmless no-opChanges
pkg/cmd/repo/clone/clone.go: AddNoUpstreamfield, register--no-upstreamflag, update fork handling logic, update help text and examplespkg/cmd/repo/clone/clone_test.go: Add flag parsing tests, mutual exclusion test, integration tests for fork/non-fork with--no-upstreamTest plan
go test ./pkg/cmd/repo/clone/...- all tests passgo vet ./pkg/cmd/repo/clone/...- no issuesgo build ./cmd/gh- builds successfullygh repo clone <fork> --no-upstreamclones without adding upstream remotegh repo clone <fork> --no-upstream --upstream-remote-name testerrors with mutual exclusion messagegh repo clone <non-fork> --no-upstreamclones normally (no-op)