Sub topic 11
Cloning with the GitHub CLI
This chapter assumes you have Git installed, a GitHub account, and basic comfort with the terminal. You should already know how git clone works with HTTPS and SSH URLs from earlier chapters.
What the GitHub CLI adds
The GitHub CLI is a command-line program named gh. It talks to GitHub on your behalf. Cloning with it still runs git clone underneath. The difference is that gh handles sign-in and builds the URL for you, so you type owner/name instead of a full address.
You install gh separately from Git. Check that it is available:
gh --versionSigning in
Run the login command once per machine:
gh auth loginThe command asks a few questions: which GitHub host to use, whether to use HTTPS or SSH for Git operations, and how to authenticate. The usual choice is to sign in through your web browser. If you pick HTTPS, gh also stores your credentials for Git, so later clones of private repositories do not prompt for a password or token. If you pick SSH, gh can upload an SSH key to your account.
You can confirm the result at any time:
gh auth statusCloning with owner/name
With sign-in done, clone using the shorthand:
gh auth login
gh repo clone octocat/Hello-WorldThis creates a Hello-World directory, the same as git clone would. The protocol follows the choice you made at login. If you chose HTTPS, gh uses the HTTPS URL. If you chose SSH, it uses the SSH URL.
To clone into a different directory, add the name after the repository:
gh repo clone octocat/Hello-World my-projectYou can also pass extra Git options after --. Everything after the double dash goes straight to git clone:
gh repo clone octocat/Hello-World -- --depth 1Forks are set up for you
When you clone a repository that is a fork of another one, gh adds the original repository as a remote named upstream automatically. In the manual workflow you would run git remote add upstream ... yourself. After cloning, check the result:
cd Hello-World
git remote -vFor a fork, you should see both origin (your fork) and upstream (the original).
How the pieces fit
When to use which
| Situation | Better choice |
|---|---|
| You already have the full URL | `git clone` |
| You want shorthand and automatic sign-in | `gh repo clone` |
| You are cloning your own fork and want `upstream` set up | `gh repo clone` |
| `gh` is not installed on the machine | `git clone` |
Common errors
gh: command not found:ghis not installed or not on yourPATH. Install it, then open a new terminal.- Not logged in: run
gh auth login, then retry. - Repository not found: check the spelling of
owner/name. For a private repository, this message can also mean your account has no access to it.
Exercise
You run gh repo clone yourname/project, and project is a fork of someone/project. Without running any other command, which remotes will git remote -v list, and how would you pass --depth 1 to the underlying clone?
Show answer
It lists origin (your fork) and upstream (someone/project), because gh adds upstream for forks. To make the clone shallow, put the option after a double dash: gh repo clone yourname/project -- --depth 1.