Since GitHub was acquired by GiantSoft, it has released a number of very useful developer tools, such as the CI/CD tools we used earlier GitHub Actions and Package packages, and today we’re going to introduce you to another useful tool that GitHub has recently released: the GitHub CLI, which allows developers to It allows developers to work seamlessly with GitHub from the command line, which means that we can pull requests, issues, and other things directly from the command line terminal. cli/cli#installation-and-upgrading) to install the GitHub CLI.

image

Installing

To install the GitHub CLI is very simple, for example, we can still install it here under macOS using the Homebrew tool:

1
2
3
$ brew install github/gh/gh
# 如果需要更新执行下面的命令即可
$ brew update && brew upgrade gh

Once the installation is complete, execute the gh command directly from the command line and the installation will be complete when you see the following message.

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
$ gh
> GET /repos/cli/cli/releases/latest
Work seamlessly with GitHub from the command line.

GitHub CLI is in early stages of development, and we'd love to hear your
feedback at <https://forms.gle/umxd3h31c7aMQFKG7>

Usage:
  gh [command]

Available Commands:
  help        Help about any command
  issue       Create and view issues
  pr          Create, view, and checkout pull requests

Flags:
      --help              Show help for command
  -R, --repo OWNER/REPO   Select another repository using the OWNER/REPO format
      --version           Show gh version

Use "gh [command] --help" for more information about a command.

For other platforms, just refer to the official documentation for installation: https://cli.github.com/manual/installation

Usage

Here’s an example of how to use the GitHub CLI, using issue and pull requests as two of the most popular features used by developers. Clone a project from GitHub and run the gh command in the project directory. For example, we’ll demonstrate this under the project that we’re blogging about here: https://github.com/cnych/qikqiak.com.

List filtering

We can use the gh command to filter issues, for example, issues with the gitment tag.

1
2
3
4
$ gh issue list  --label "gitment"
> GET /repos/cli/cli/releases/latest
Notice: authentication required
Press Enter to open github.com in your browser... < HTTP 200 OK

The first time we use it, we need to authorize it once, enter the Enter key in the command line to open the authorization page in the browser, click on the authorization to.

image

Once the authorization is complete go back to the terminal and type enter to get the result.

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
$ gh issue list  --label "gitment"
[git remote -v]
> GET /repos/cli/cli/releases/latest
> POST /graphql
< HTTP 200 OK
< HTTP 200 OK

Issues for cnych/qikqiak.com

> POST /graphql
< HTTP 200 OK
#152  Kubernetes 零宕机滚动更新                                          (gitment, zero-downtime-rolling-update-k8s)
#151  在 Kubernetes 集群上部署 VSCode                                   (deploy-vscode-on-k8s, gitment)
#150  自定义 Traefik2 中间件                                            (custom-traefik2-middleware, gitment)
#149  基于 Jenkins 的 DevOps 流水线实践                                   (devops-base-on-jenkins, gitment)
#148  自定义 Kubernetes 调度器                                          (custom-kube-scheduler, gitment)
#146  一文搞懂 Traefik2.1 的使用                                         (gitment, traefik-2.1-101)
......

The above command will filter out issues with the gitment tag.

Quick View Details

After finding an issue of interest, to see the details of the issue, you can quickly open the issue details page in your browser with the following command.

1
2
3
4
5
6
7
8
$ gh issue view 152
[git remote -v]
> POST /graphql
< HTTP 200 OK
> POST /graphql
< HTTP 200 OK
Opening https://github.com/cnych/qikqiak.com/issues/152 in your browser.
[open https://github.com/cnych/qikqiak.com/issues/152]

Creating a PR

After creating a branch and fixing the bug described in the issue after committing the code a few times, you can then use the gh command to create a pull request to commit our contributed code.

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
gh pr create
[git remote -v]
> POST /graphql
< HTTP 200 OK
[git rev-parse --abbrev-ref HEAD]
[git status --porcelain]
[git push --set-upstream origin HEAD:gh-pages]

Creating pull request for gh-pages into master in cnych/qikqiak.com

[git rev-parse --show-toplevel]
? Title Update gitignore
? Body <Received>
? What's next?  [Use arrows to move, type to filter]
> Preview in browser
  Submit
  Cancel

We can also use the up and down arrow keys on the keyboard to move for action selection, to jump to the browser for action, or to submit or cancel directly.

Status view

To quickly see the status of a pull request after it has been created, you can also use the gh command to display the review and status of the pull requests.

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
$ gh pr status
[git remote -v]
[git rev-parse --abbrev-ref HEAD]
[git config --get-regexp ^branch\.gh-pages\.(remote|merge)$]
> POST /graphql
< HTTP 200 OK
> POST /graphql
< HTTP 200 OK

Relevant pull requests in cnych/qikqiak.com

Current branch
  #153  Update gitignore rules [gh-pages]
   - Checks passing

Created by you
  #153  Update gitignore rules [gh-pages]
   - Checks passing

Requesting a code review from you
  You have no pull requests to review

Here we just briefly introduce a few common commands for issue and pull requests, for more usage you can check the official documentation to learn more: https://cli.github.com/manual/examples.