The GitHub CLI

Updated · 10 min read
easyGit 2.53gitgithub-clighglabpull-request

A bank's mobile app does what the branch counter does: check the balance, send money, download a statement. You still have the same account, but you don't have to go to the branch for everyday tasks.

gh, the GitHub command-line tool, is that app for GitHub. Git already handles commits, branches, push and pull, but pull requests, issues, forks and repository settings live on GitHub's website. gh brings them to your terminal: you can open a pull request right after pushing, check out a teammate's pull request by its number, or merge one, without switching to the browser. It is optional, and everything it does can also be done on the website.

In this lesson, you will install and sign in to gh, and use it for the tasks from the previous lessons: creating and cloning repositories, forking, and the whole life of a pull request. At the end, you will meet glab, GitLab's equivalent.

Install and Sign In

Shell
winget install --id GitHub.cli

Then sign in once:

Terminal
gh auth login

gh asks a few questions: the account (GitHub.com), the protocol for Git (HTTPS or SSH), and how to sign in (Login with a web browser is easiest: it shows a one-time code to enter on a GitHub page). With HTTPS, gh also sets itself up as Git's credential helper, so git push stops asking for a token. gh auth status shows who you are signed in as.

Unlike the other lessons in this course, the commands here need a real GitHub account, so their results aren't shown as checked output. Try them on a practice repository of your own.

Repositories

CommandIt is used to
gh repo create fee-tracker --private --source=. --remote=origin --pushCreate a GitHub repository from the current local project, add it as origin and push
gh repo clone <owner>/<repo>Clone by name, without copying a URL
gh repo fork <owner>/<repo> --cloneFork a repository, clone your fork, and add the original as upstream
gh repo sync <you>/<fork>Bring your fork's default branch on GitHub up to date with the original (with no argument, it updates the local copy's default branch instead)
gh repo view --webOpen the current repository in the browser

gh repo create with --source=. does in one line what "Getting Started on GitHub, GitLab and Bitbucket" did in several: create the empty repository, git remote add origin, and git push -u origin main. And gh repo fork --clone does the fork, clone and upstream steps of "Forking a Repository".

The Life of a Pull Request with gh

The same steps as in "Pull Requests and Merge Requests", from the terminal:

Terminal
# 1. Branch, commit, push (plain Git, as always)
git switch -c feature/late-fee
git commit -am "Add the late fee"
git push -u origin feature/late-fee
 
# 2. Open the pull request
gh pr create --fill

--fill uses the commit subjects and bodies as the pull request's title and description. Without it, gh asks for a title, opens your editor for the description, and offers to submit or to continue in the browser. Other useful options:

Option of gh pr createIt is used to
--title "..." --body "..."Give the title and description directly
--draftOpen it as a draft
--base <branch>Target a branch other than the default
--reviewer <user>Ask someone to review
--webFinish creating it in the browser

Then, for everyone on the team:

CommandIt is used to
gh pr listList the open pull requests
gh pr statusShow your pull requests and those waiting for your review
gh pr view 12Show pull request 12 (add --web for the browser)
gh pr diff 12Show its changes
gh pr checkout 12Fetch its branch and switch to it, even if it comes from a fork
gh pr review 12 --approveApprove it (or --request-changes -b "...", --comment -b "...")
gh pr checks 12Show the status of its automated checks
gh pr merge 12 --squash --delete-branchMerge it (or --merge, --rebase) and delete the branch

gh pr checkout is the friendly version of the git fetch origin pull/<n>/head command from the previous lessons, and gh pr merge presses the merge button for you. Protected-branch rules still apply.

A pull request from the terminal

Issues and Other Tasks

CommandIt is used to
gh issue create --title "..." --body "..."Report a bug or plan a task
gh issue listList open issues
gh issue view 7Read issue 7
gh browseOpen the repository's page in the browser
gh api repos/<owner>/<repo>Call GitHub's API directly, for scripts
gh alias set prs "pr list --author @me"Make your own short commands

GitLab and Bitbucket

GitLab has its own command-line tool, glab, which works the same way for merge requests:

GitHub (gh)GitLab (glab)
gh auth loginglab auth login
gh repo clone <owner>/<repo>glab repo clone <group>/<project>
gh pr create --fillglab mr create --fill
gh pr listglab mr list
gh pr checkout 12glab mr checkout 12
gh pr merge 12glab mr merge 12
gh issue listglab issue list

For Bitbucket, the usual ways to work with pull requests outside the browser are IDE integrations (such as the Atlassian extensions for VS Code and IntelliJ IDEA) and Bitbucket's REST API.

Common Mistakes

Using gh for things Git does. gh doesn't replace git commit, git push or git pull. It only adds GitHub's features.

Running gh outside a repository. Most commands work on "the current repository", found from origin. Run them inside the project, or name it with -R <owner>/<repo>.

Being signed in to the wrong account. gh auth status shows the account. gh auth switch changes between accounts you are signed in to.

Merging with gh pr merge before checks finish. Look at gh pr checks first, or let protected-branch rules make the merge wait.

Under the Hood

gh is a client for GitHub's web API. Each command sends an HTTPS request to api.github.com with the token it stored at gh auth login, the same way the website does behind its buttons. For Git work, it runs the normal git program: gh pr checkout runs git fetch and git switch, and gh repo clone runs git clone. That is why everything you learned about Git stays true when you use gh.

Interview Corner

What is the GitHub CLI?

gh, GitHub's official command-line tool. It works with GitHub features that aren't part of Git, such as pull requests, issues, forks, releases and the API, from the terminal. Git itself still does commits, branches, pushes and pulls.

How do you check out a pull request locally with gh?

gh pr checkout <number>. It fetches the pull request's branch, also from a fork, creates a local branch and switches to it.

How do you create a GitHub repository from an existing local project with gh?

gh repo create <name> --private --source=. --remote=origin --push creates the repository, adds it as origin and pushes the current branch.

What is GitLab's equivalent of gh?

glab, which uses mr (merge request) where gh uses pr, for example glab mr create and glab mr checkout.

Practice

Challenge: the whole flow from the terminal. On a practice repository of your own on GitHub, do the full life of a pull request without opening the browser:

  1. Sign in with gh auth login and check it with gh auth status.
  2. Create a branch, change a file, commit and push it with -u.
  3. Open a pull request with gh pr create --fill, and list it with gh pr list.
  4. Check the pull request out in a second clone with gh pr checkout <number>.
  5. Merge it with gh pr merge <number> --squash --delete-branch.
  6. Update your local main with git switch main and git pull --prune, and delete the local branch with git branch -D.

Think about it. Before step 6, decide why git branch -d would refuse to delete the branch, and why -D is safe here.

The merge in step 5 was a squash, so the branch's own commits aren't in main, only one new commit with the same changes. git branch -d can't see that the work is merged and refuses, as in "Pull Requests and Merge Requests". After you check that the squash commit is on main with git log --oneline -1, -D deletes a branch whose work is safely in main.

Checkpoint. Fork a public repository with gh repo fork <owner>/<repo> --clone, check that upstream was added with git remote -v, and keep the fork in sync with gh repo sync <you>/<fork> (which updates the fork on GitHub) followed by git pull.

Quick check: The GitHub CLI

4 questions
1Which command opens a pull request from the current branch?
2What does gh pr checkout 12 do?
3Which tasks still need Git, even with gh installed?
4What is GitLab's command for creating a merge request from the terminal?

Key Takeaways

Quick reference

CommandIt is used to
gh auth loginSign in to GitHub
gh repo create <name> --source=. --pushPublish the current project
gh repo fork <owner>/<repo> --cloneFork and clone with upstream
gh pr create --fillOpen a pull request
gh pr checkout <n>Check out a pull request
gh pr merge <n> --squash --delete-branchMerge a pull request
gh issue listList issues
glab mr create --fillOpen a GitLab merge request

Was this tutorial helpful?

Lesson 50 of 106