The GitHub CLI
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
winget install --id GitHub.clibrew install gh# Packages for Debian, Ubuntu, Fedora and others: see cli.github.com
sudo apt install ghThen sign in once:
gh auth logingh 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
| Command | It is used to |
|---|---|
gh repo create fee-tracker --private --source=. --remote=origin --push | Create 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> --clone | Fork 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 --web | Open 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:
# 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 create | It is used to |
|---|---|
--title "..." --body "..." | Give the title and description directly |
--draft | Open it as a draft |
--base <branch> | Target a branch other than the default |
--reviewer <user> | Ask someone to review |
--web | Finish creating it in the browser |
Then, for everyone on the team:
| Command | It is used to |
|---|---|
gh pr list | List the open pull requests |
gh pr status | Show your pull requests and those waiting for your review |
gh pr view 12 | Show pull request 12 (add --web for the browser) |
gh pr diff 12 | Show its changes |
gh pr checkout 12 | Fetch its branch and switch to it, even if it comes from a fork |
gh pr review 12 --approve | Approve it (or --request-changes -b "...", --comment -b "...") |
gh pr checks 12 | Show the status of its automated checks |
gh pr merge 12 --squash --delete-branch | Merge 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.
Issues and Other Tasks
| Command | It is used to |
|---|---|
gh issue create --title "..." --body "..." | Report a bug or plan a task |
gh issue list | List open issues |
gh issue view 7 | Read issue 7 |
gh browse | Open 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 login | glab auth login |
gh repo clone <owner>/<repo> | glab repo clone <group>/<project> |
gh pr create --fill | glab mr create --fill |
gh pr list | glab mr list |
gh pr checkout 12 | glab mr checkout 12 |
gh pr merge 12 | glab mr merge 12 |
gh issue list | glab 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:
- Sign in with
gh auth loginand check it withgh auth status. - Create a branch, change a file, commit and push it with
-u. - Open a pull request with
gh pr create --fill, and list it withgh pr list. - Check the pull request out in a second clone with
gh pr checkout <number>. - Merge it with
gh pr merge <number> --squash --delete-branch. - Update your local
mainwithgit switch mainandgit pull --prune, and delete the local branch withgit 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.
Key Takeaways
Quick reference
| Command | It is used to |
|---|---|
gh auth login | Sign in to GitHub |
gh repo create <name> --source=. --push | Publish the current project |
gh repo fork <owner>/<repo> --clone | Fork and clone with upstream |
gh pr create --fill | Open a pull request |
gh pr checkout <n> | Check out a pull request |
gh pr merge <n> --squash --delete-branch | Merge a pull request |
gh issue list | List issues |
glab mr create --fill | Open a GitLab merge request |
Was this tutorial helpful?