Sub topic 3
Cloning, creating, viewing, and forking repositories
This chapter assumes you have finished the authentication chapter, so gh auth status shows a logged-in account. It also assumes you know basic Git: clone, remote, and push. Here you will use gh repo to get a repository onto your machine, create new ones, inspect them, and fork other people's.
The repo command group
All repository operations live under gh repo. The main subcommands are clone, create, view, fork, and list. Each accepts a repository in OWNER/NAME form. Where it makes sense, gh repo also infers the repository from the current directory's Git remotes.
Cloning and viewing
gh repo clone wraps git clone. It resolves OWNER/NAME to a URL using your configured protocol (HTTPS or SSH), so you do not have to build the URL yourself.
gh repo clone cli/cli
cd cli
gh repo viewInside a clone, gh repo view prints the description and README of the repository that the remotes point to. Outside a clone, pass the name: gh repo view cli/cli. Add --web to open the repository in your browser instead.
If you give only a name, as in gh repo clone cli, gh assumes the owner is your authenticated account.
Creating a repository
gh repo create has three common forms. Choose one by where your code lives now.
# 1. New repository on GitHub, cloned locally
gh repo create my-project --public --clone
# 2. Existing local directory, published to GitHub
cd existing-project
gh repo create my-project --private --source=. --remote=origin --push
# 3. Description and visibility set up front
gh repo create team-notes --private --description "Meeting notes" --add-readmeWithout flags, gh repo create becomes interactive and asks questions. In scripts, always pass an explicit visibility flag (--public, --private, or --internal) so the command never prompts.
Forking
A fork is your own copy of someone else's repository. You can push to it, and you can open pull requests from it back to the original.
gh repo fork cli/cli --clone
cd cli
git remote -vWith --clone, gh clones your fork and sets up two remotes:
| Remote | Points to |
|---|---|
| `origin` | Your fork |
| `upstream` | The original repository |
If you run gh repo fork inside an existing clone of the original, add --remote to create the same upstream setup without a prompt.
Listing your repositories
gh repo list shows repositories for you or any owner. Use --limit to control how many appear.
gh repo list --limit 10
gh repo list cli --limit 5Common errors
- Clone fails with a permission or authentication error. Run
gh auth status. Check that the active account can see the repository, since private repositories are invisible to the wrong account. gh repo viewreports it cannot determine the repository. You are outside a clone, or the clone has no GitHub remote. PassOWNER/NAMEexplicitly.- Wrong protocol. gh clones with the protocol you chose at login. Change it with
gh config set git_protocol ssh. gh repo createfails because the name exists. Repository names must be unique per owner. Pick another name, or useOWNER/NAMEto create it under an organization you belong to.
Exercise
You cloned a project you do not own, and you want to push changes to your own copy and open a pull request. Which gh command sets this up, and which remote name will point to the original repository?
Show answer
Run gh repo fork OWNER/NAME --clone, or gh repo fork --remote inside an existing clone. The origin remote points to your fork, and upstream points to the original repository.
Exercise
You have a local directory with commits and no GitHub repository yet. Write a single command that creates a private repository named notes, uses the current directory as its source, and pushes the commits.
Show answer
gh repo create notes --private --source=. --push