Sub topic 14
Doing the Same with the GitHub CLI
This chapter assumes you have finished the browser-based workflow from earlier chapters. You should be comfortable with forks, the origin and upstream remotes, feature branches, and pull requests. It also assumes Git is installed and you can use a terminal.
What the GitHub CLI is
The GitHub CLI (gh) is a command-line program from GitHub. It talks to the GitHub API on your behalf. Git handles your local history and pushes. gh handles GitHub-specific steps such as forking a repository or opening a pull request, so you do not need to switch to the website.
Install it from [cli.github.com](https://cli.github.com), then sign in once:
gh auth login
gh auth statusgh auth login asks which host to use and how to authenticate. gh auth status confirms that you are signed in.
Fork and clone in one step
In the browser workflow, you forked on the website, cloned your fork, and added upstream by hand. gh repo fork does all three with one flag:
gh repo fork ORIGINAL-OWNER/REPO --clone
cd REPO
git remote -vThe command creates the fork under your account and clones it. It also names your fork origin and the original repository upstream. Check the output of git remote -v to confirm both remotes exist.
If you run gh repo fork from inside an existing clone of the original repository, it offers to add a remote for your new fork instead.
Branch, commit, and push
These steps use plain Git, exactly as before:
git switch -c fix-readme-typo
# edit README.md
git add README.md
git commit -m "Fix typo in README installation section"
git push -u origin fix-readme-typogh does not replace these commands. It starts working again when you open the pull request.
Open the pull request
Run gh pr create from your feature branch:
gh pr create --title "Fix typo in README installation section" --body "Corrects a misspelled command in the install steps."If your branch is not yet pushed, gh offers to push it. Because the repository is a fork, gh targets the upstream repository as the base and your branch as the head. If you leave out --title and --body, it prompts you for them. For a longer description, use --web to finish the form in your browser.
Check on your pull request
Several commands let you follow the review without leaving the terminal:
| Command | What it does |
|---|---|
| `gh pr status` | Shows pull requests you created and ones that need your attention |
| `gh pr view --comments` | Prints the description and review comments |
| `gh pr checks` | Lists the status of automated checks |
| `gh pr view --web` | Opens the pull request in your browser |
To respond to feedback, commit on the same branch and run git push. The open pull request updates automatically, as it did in the browser workflow.
Common errors
gh: command not found: the CLI is not installed or is not on yourPATH.- Authentication errors when pushing:
gh auth loginsets up credentials forgh. Git pushes use your Git credential setup. Rungh auth setup-gitto let Git reuse theghlogin over HTTPS. - The pull request targets your own fork: pass
--repo ORIGINAL-OWNER/REPOtogh pr create, or check that your remotes are set asoriginandupstream.
Exercise
You ran gh repo fork ORIGINAL-OWNER/REPO --clone. Which remote names does the clone have, and which command would you use to open a pull request after pushing your branch?
Show answer
The clone has origin, which points to your fork, and upstream, which points to the original repository. After pushing the branch with git push -u origin BRANCH, run gh pr create from that branch.