Skip to content
LatchkeyLatchkey home

gh repo fork: Fork a Repository in CI

gh repo fork creates a fork of a repository under your account and can clone it locally.

Contribution bots fork upstream, branch, and open a PR; gh repo fork handles the fork and remote wiring in one step.

What it does

gh repo fork forks the named repository (or the current one) into your account or an organization. With --clone it clones the fork, and --remote configures an upstream remote pointing at the original repository.

Common usage

Terminal
gh repo fork cli/cli --clone
gh repo fork cli/cli --org my-org --clone=false
gh repo fork --remote --remote-name upstream

Flags

FlagWhat it does
--cloneClone the fork after creating it
--remoteAdd a git remote for the fork
--remote-name <name>Name for the upstream remote (default upstream)
--fork-name <name>Rename the fork
--org <name>Create the fork in an organization
--default-branch-onlyOnly fork the default branch

In CI

Set GH_TOKEN. The default GITHUB_TOKEN cannot fork to a personal account or another org, so forking in Actions requires a PAT or App token with repo and the right org scope. Forking is asynchronous; the fork may take a few seconds to be ready to clone.

Common errors in CI

"Resource not accessible by integration" means GITHUB_TOKEN was used where forking needs a PAT. "HTTP 403" can mean forking is disabled for the org. If --clone runs before the fork is fully created, the clone may briefly fail; gh retries, but a custom script should add a short wait.

Using this in CI

CI checkouts are shallow and detached by default, which changes the answer this command gives you. Commands that read history, branch names, or tags need the checkout configured for it.

.github/workflows/ci.yml
- uses: actions/checkout@v4
  with:
    fetch-depth: 0   # history, tags, and git describe all need this

- run: |
    git rev-parse --is-shallow-repository   # expect false
    git rev-parse --abbrev-ref HEAD          # prints HEAD when detached

Frequently asked questions

gh repo fork: Fork a Repository in CI?
Contribution bots fork upstream, branch, and open a PR; gh repo fork handles the fork and remote wiring in one step.
What it does?
gh repo fork forks the named repository (or the current one) into your account or an organization. With --clone it clones the fork, and --remote configures an upstream remote pointing at the original repository.
In CI?
Set GH_TOKEN. The default GITHUB_TOKEN cannot fork to a personal account or another org, so forking in Actions requires a PAT or App token with repo and the right org scope. Forking is asynchronous; the fork may take a few seconds to be ready to clone.
Common errors in CI?
"Resource not accessible by integration" means GITHUB_TOKEN was used where forking needs a PAT. "HTTP 403" can mean forking is disabled for the org. If --clone runs before the fork is fully created, the clone may briefly fail; gh retries, but a custom script should add a short wait.

Related guides

References

Run this faster and cheaper on Latchkey managed runners - self-healing included. Start free → 30-day trial · No credit card